Rest api » History » Version 84
Jean-Philippe Lang, 2012-10-17 22:28
1 | 26 | Jean-Philippe Lang | {{>toc}} |
---|---|---|---|
2 | |||
3 | 1 | Jean-Philippe Lang | h1. Redmine API |
4 | |||
5 | 60 | Jean-Philippe Lang | Redmine exposes some of its data through a REST API. This API provides access and basic CRUD operations (create, update, delete) for the resources described below. The API supports both "XML":http://en.wikipedia.org/wiki/Xml and "JSON":http://en.wikipedia.org/wiki/JSON formats. |
6 | 1 | Jean-Philippe Lang | |
7 | h2. API Description |
||
8 | |||
9 | 24 | Jean-Philippe Lang | |_.Resource |_.Status |_.Notes |_.Availability| |
10 | 56 | Jean-Philippe Lang | |[[Rest_Issues|Issues]] | Stable | Usable with some bugs and rough edges. | 1.0 | |
11 | |[[Rest_Projects|Projects]] | Stable | Usable with some bugs and rough edges. | 1.0 | |
||
12 | 55 | Jean-Philippe Lang | |[[Rest_Memberships|Project Memberships]] | Alpha | | 1.4 | |
13 | 56 | Jean-Philippe Lang | |[[Rest_Users|Users]] | Stable | | 1.1 | |
14 | |[[Rest_TimeEntries|Time Entries]] | Stable | | 1.1 | |
||
15 | 28 | Jean-Philippe Lang | |[[Rest_News|News]] | Prototype | Prototype implementation for @index@ only | 1.1 | |
16 | 43 | Jean-Philippe Lang | |[[Rest_IssueRelations|Issue Relations]] | Alpha | | 1.3 | |
17 | |[[Rest_Versions|Versions]] | Alpha | | 1.3 | |
||
18 | 44 | Jean-Philippe Lang | |[[Rest_Queries|Queries]] | Alpha | | 1.3 | |
19 | 63 | Jean-Philippe Lang | |[[Rest_Attachments|Attachments]] | Beta | Adding attachments via the API added in 1.4 | 1.3 | |
20 | 53 | Jean-Philippe Lang | |[[Rest_IssueStatuses|Issue Statuses]] | Alpha | Provides the list of all statuses | 1.3 | |
21 | 51 | Jean-Philippe Lang | |[[Rest_Trackers|Trackers]] | Alpha | Provides the list of all trackers | 1.3 | |
22 | 84 | Jean-Philippe Lang | |[[Rest_Enumerations|Enumerations]] | Alpha | Provides the list of issue priorities and time tracking activities | 2.2 | |
23 | 52 | Jean-Philippe Lang | |[[Rest_IssueCategories|Issue Categories]] | Alpha | | 1.3 | |
24 | 55 | Jean-Philippe Lang | |[[Rest_Roles|Roles]] | Alpha | | 1.4 | |
25 | 79 | Jean-Philippe Lang | |[[Rest_Groups|Groups]] | Alpha | | 2.1 | |
26 | 24 | Jean-Philippe Lang | |
27 | 15 | Eric Davis | Status legend: |
28 | 1 | Jean-Philippe Lang | |
29 | * Stable - feature complete, no major changes planned |
||
30 | * Beta - usable for integrations with some bugs or missing minor functionality |
||
31 | * Alpha - major functionality in place, needs feedback from API users and integrators |
||
32 | * Prototype - very rough implementation, possible major breaking changes mid-version. *Not recommended for integration* |
||
33 | * Planned - planned in a future version, depending on developer availability |
||
34 | |||
35 | 24 | Jean-Philippe Lang | h2. General topics |
36 | 1 | Jean-Philippe Lang | |
37 | 78 | Jean-Philippe Lang | h3. Specify @Content-Type@ on @POST@/@PUT@ requests |
38 | 66 | Etienne Massip | |
39 | 83 | Jean-Philippe Lang | When creating or updating a remote element, the @Content-Type@ of the request *MUST* be specified even if the remote URL is suffixed accordingly (e.g. @POST ../issues.json@): |
40 | 82 | Jean-Philippe Lang | * for JSON content, it must be set to @Content-Type: application/json@. |
41 | 78 | Jean-Philippe Lang | * for XML content, to @Content-Type: application/xml@. |
42 | 66 | Etienne Massip | |
43 | 24 | Jean-Philippe Lang | h3. Authentication |
44 | |||
45 | Most of the time, the API requires authentication. To enable the API-style authentication, you have to check *Enable REST API* in Administration -> Settings -> Authentication. Then, authentication can be done in 2 different ways: |
||
46 | * using your regular login/password via HTTP Basic authentication. |
||
47 | 38 | Jean-Philippe Lang | * using your API key which is a handy way to avoid putting a password in a script. The API key may be attached to each request in one of the following way: |
48 | ** passed in as a "key" parameter |
||
49 | ** passed in as a username with a random password via HTTP Basic authentication |
||
50 | 46 | John Galambos | ** passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0) |
51 | 38 | Jean-Philippe Lang | |
52 | You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout. |
||
53 | 24 | Jean-Philippe Lang | |
54 | h3. Collection resources and pagination |
||
55 | |||
56 | 47 | Tom Clegg | The response to a GET request on a collection ressources (eg. @/issues.xml@, @/users.xml@) generally won't return all the objects available in your database. Redmine version:1.1.0 introduces a common way to query such ressources using the following parameters: |
57 | 24 | Jean-Philippe Lang | |
58 | * @offset@: the offset of the first object to retrieve |
||
59 | * @limit@: the number of items to be present in the response (default is 25, maximum is 100) |
||
60 | |||
61 | Examples: |
||
62 | |||
63 | <pre> |
||
64 | GET /issues.xml |
||
65 | => returns the 25 first issues |
||
66 | |||
67 | GET /issues.xml?limit=100 |
||
68 | => returns the 100 first issues |
||
69 | |||
70 | GET /issues.xml?offset=30&limit=10 |
||
71 | => returns 10 issues from the 30th |
||
72 | </pre> |
||
73 | |||
74 | Responses to GET requests on collection ressources provide information about the total object count available in Redmine and the offset/limit used for the response. Examples: |
||
75 | |||
76 | <pre> |
||
77 | GET /issues.xml |
||
78 | |||
79 | <issues type="array" total_count="2595" limit="25" offset="0"> |
||
80 | ... |
||
81 | </issues> |
||
82 | </pre> |
||
83 | |||
84 | <pre> |
||
85 | GET /issues.json |
||
86 | |||
87 | { "issues":[...], "total_count":2595, "limit":25, "offset":0 } |
||
88 | </pre> |
||
89 | |||
90 | Note: if you're using a REST client that does not support such top level attributes (total_count, limit, offset), you can set the @nometa@ parameter or @X-Redmine-Nometa@ HTTP header to 1 to get responses without them. Example: |
||
91 | |||
92 | <pre> |
||
93 | GET /issues.xml?nometa=1 |
||
94 | |||
95 | <issues type="array"> |
||
96 | ... |
||
97 | </issues> |
||
98 | </pre> |
||
99 | 23 | Jean-Philippe Lang | |
100 | 29 | Etienne Massip | h3. Fetching associated data |
101 | |||
102 | Since of version:1.1.0, you have to explicitly specify the associations you want to be included in the query result by appending the @include@ parameter to the query url : |
||
103 | |||
104 | Example: |
||
105 | |||
106 | 41 | Jean-Philippe Lang | To retrieve issue journals with its description: |
107 | 29 | Etienne Massip | |
108 | <pre> |
||
109 | GET /issues/296.xml?include=journals |
||
110 | |||
111 | <issue> |
||
112 | <id>296</id> |
||
113 | 30 | Etienne Massip | ... |
114 | 29 | Etienne Massip | <journals type="array"> |
115 | ... |
||
116 | 1 | Jean-Philippe Lang | </journals> |
117 | 41 | Jean-Philippe Lang | </issue> |
118 | </pre> |
||
119 | |||
120 | You can also load multiple associations using a coma separated list of items. |
||
121 | |||
122 | Example: |
||
123 | |||
124 | <pre> |
||
125 | GET /issues/296.xml?include=journals,changesets |
||
126 | |||
127 | <issue> |
||
128 | <id>296</id> |
||
129 | ... |
||
130 | <journals type="array"> |
||
131 | ... |
||
132 | </journals> |
||
133 | <changesets type="array"> |
||
134 | ... |
||
135 | </changesets> |
||
136 | 29 | Etienne Massip | </issue> |
137 | </pre> |
||
138 | |||
139 | 42 | Jean-Philippe Lang | h3. Working with custom fields |
140 | |||
141 | Most of the Redmine objects support custom fields. Their values can be found in the @custom_fields@ attributes. |
||
142 | |||
143 | XML Example: |
||
144 | |||
145 | <pre> |
||
146 | GET /issues/296.xml # an issue with 2 custom fields |
||
147 | |||
148 | <issue> |
||
149 | <id>296</id> |
||
150 | ... |
||
151 | <custom_fields type="array"> |
||
152 | <custom_field name="Affected version" id="1"> |
||
153 | <value>1.0.1</value> |
||
154 | </custom_field> |
||
155 | <custom_field name="Resolution" id="2"> |
||
156 | <value>Fixed</value> |
||
157 | </custom_field> |
||
158 | </custom_fields> |
||
159 | </issue> |
||
160 | </pre> |
||
161 | |||
162 | JSON Example: |
||
163 | |||
164 | <pre> |
||
165 | GET /issues/296.json # an issue with 2 custom fields |
||
166 | |||
167 | {"issue": |
||
168 | { |
||
169 | "id":8471, |
||
170 | ... |
||
171 | "custom_fields": |
||
172 | [ |
||
173 | {"value":"1.0.1","name":"Affected version","id":1}, |
||
174 | {"value":"Fixed","name":"Resolution","id":2} |
||
175 | ] |
||
176 | } |
||
177 | } |
||
178 | </pre> |
||
179 | |||
180 | You can also set/change the values of the custom fields when creating/updating an object using the same syntax (except that the custom field name is not required). |
||
181 | |||
182 | XML Example: |
||
183 | |||
184 | <pre> |
||
185 | PUT /issues/296.xml |
||
186 | |||
187 | <issue> |
||
188 | <subject>Updating custom fields of an issue</subject> |
||
189 | ... |
||
190 | <custom_fields type="array"> |
||
191 | <custom_field id="1"> |
||
192 | <value>1.0.2</value> |
||
193 | </custom_field> |
||
194 | <custom_field id="2"> |
||
195 | <value>Invalid</value> |
||
196 | </custom_field> |
||
197 | </custom_fields> |
||
198 | </issue> |
||
199 | </pre> |
||
200 | |||
201 | Note: the @type="array"@ attribute on @custom_fields@ XML tag is strictly required. |
||
202 | |||
203 | JSON Example: |
||
204 | |||
205 | <pre> |
||
206 | PUT /issues/296.json |
||
207 | |||
208 | {"issue": |
||
209 | { |
||
210 | "subject":"Updating custom fields of an issue", |
||
211 | ... |
||
212 | "custom_fields": |
||
213 | [ |
||
214 | {"value":"1.0.2","id":1}, |
||
215 | {"value":"Invalid","id":2} |
||
216 | ] |
||
217 | } |
||
218 | } |
||
219 | </pre> |
||
220 | |||
221 | 61 | Jean-Philippe Lang | h3. Attaching files |
222 | |||
223 | Support for adding attachments through the REST API is added in Redmine version:1.4.0. |
||
224 | |||
225 | First, you need to upload your file with a POST request to @/uploads.xml@ (or @/uploads.json@). The request body should be the content of the file you want to attach and the @Content-Type@ header must be set to @application/octet-stream@ (otherwise you'll get a @406 Not Acceptable@ response). If the upload succeeds, you get a 201 response that contains a token for your uploaded file. |
||
226 | |||
227 | <pre> |
||
228 | POST /uploads.xml |
||
229 | Content-Type: application/octet-stream |
||
230 | ... |
||
231 | (request body is the file content) |
||
232 | |||
233 | # 201 response |
||
234 | <upload> |
||
235 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
236 | </upload> |
||
237 | </pre> |
||
238 | |||
239 | Then you can use this token to attach your uploaded file to a new or an existing issue. |
||
240 | |||
241 | <pre> |
||
242 | POST /issues.xml |
||
243 | <issue> |
||
244 | <project_id>1</project_id> |
||
245 | <subject>Creating an issue with a uploaded file</subject> |
||
246 | 62 | Jean-Philippe Lang | <uploads type="array"> |
247 | 61 | Jean-Philippe Lang | <upload> |
248 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
249 | <filename>image.png</filename> |
||
250 | <content_type>image/png</content_type> |
||
251 | </upload> |
||
252 | </uploads> |
||
253 | </issue> |
||
254 | </pre> |
||
255 | |||
256 | 64 | Jean-Philippe Lang | If you try to upload a file that exceeds the maximum size allowed, you get a 422 response: |
257 | |||
258 | <pre> |
||
259 | POST /uploads.xml |
||
260 | Content-Type: application/octet-stream |
||
261 | ... |
||
262 | (request body larger than the maximum size allowed) |
||
263 | |||
264 | # 422 response |
||
265 | <errors> |
||
266 | <error>This file cannot be uploaded because it exceeds the maximum allowed file size (1024000)</error> |
||
267 | </errors> |
||
268 | </pre> |
||
269 | |||
270 | 59 | Jean-Philippe Lang | h3. Validation errors |
271 | |||
272 | When trying to create or update an object with invalid or missing attribute parameters, you will get a @422 Unprocessable Entity@ response. That means that the object could not be created or updated. In such cases, the response body contains the corresponding error messages: |
||
273 | |||
274 | +XML Example+: |
||
275 | |||
276 | <pre> |
||
277 | # Request with invalid or missing attributes |
||
278 | POST /users.xml |
||
279 | <user> |
||
280 | <login>john</login> |
||
281 | <lastname>Smith</lastname> |
||
282 | <mail>john</mail> |
||
283 | </uer> |
||
284 | |||
285 | # 422 response with the error messages in its body |
||
286 | 65 | Jean-Philippe Lang | <errors type="array"> |
287 | 59 | Jean-Philippe Lang | <error>First name can't be blank</error> |
288 | <error>Email is invalid</error> |
||
289 | </errors> |
||
290 | </pre> |
||
291 | |||
292 | |||
293 | +JSON Example+: |
||
294 | |||
295 | <pre> |
||
296 | # Request with invalid or missing attributes |
||
297 | POST /users.json |
||
298 | { |
||
299 | "user":{ |
||
300 | "login":"john", |
||
301 | "lastname":"Smith", |
||
302 | "mail":"john" |
||
303 | } |
||
304 | } |
||
305 | |||
306 | # 422 response with the error messages in its body |
||
307 | { |
||
308 | "errors":[ |
||
309 | "First name can't be blank", |
||
310 | "Email is invalid" |
||
311 | ] |
||
312 | } |
||
313 | </pre> |
||
314 | |||
315 | 81 | Jean-Philippe Lang | h3. JSONP Support |
316 | |||
317 | Redmine 2.1.0+ API supports "JSONP":http://en.wikipedia.org/wiki/JSONP to request data from a Redmine server in a different domain (say, with JQuery). The callback can be passed using the @callback@ or @jsonp@ parameter. |
||
318 | |||
319 | Example: |
||
320 | |||
321 | <pre> |
||
322 | GET /issues.json?callback=myHandler |
||
323 | |||
324 | myHandler({"issues":[ ... ]}) |
||
325 | </pre> |
||
326 | |||
327 | 1 | Jean-Philippe Lang | h2. API Usage in various languages/tools |
328 | 5 | Jean-Philippe Lang | |
329 | 1 | Jean-Philippe Lang | * [[Rest_api_with_ruby|Ruby]] |
330 | * [[Rest_api_with_php|PHP]] |
||
331 | 23 | Jean-Philippe Lang | * [[Rest_api_with_python|Python]] |
332 | 27 | Jean-Philippe Lang | * [[Rest_api_with_java|Java]] |
333 | 1 | Jean-Philippe Lang | * [[Rest_api_with_curl|cURL]] |
334 | 37 | Bevan Rudge | * "Drupal Redmine API module, 2.x branch (currently not stable)":http://drupal.org/project/redmine |
335 | 48 | Dorin Huzum | * [[Rest_api_with_csharp|.NET]] |
336 | 49 | Rodrigo Carvalho | * [[Rest_api_with_delphi|Delphi]] |
337 | 54 | Jean-Philippe Lang | |
338 | h2. API Change history |
||
339 | |||
340 | 58 | Jean-Philippe Lang | This section lists changes to the existing API features only. New features of the API are listed in the [[Rest_api#API-Description|API Description]]. |
341 | 57 | Jean-Philippe Lang | |
342 | 54 | Jean-Philippe Lang | h3. 2012-01-29: Multiselect custom fields (r8721, version:1.4.0) |
343 | |||
344 | Custom fields with multiple values are now supported in Redmine and may be found in API responses. These custom fields have a @multiple=true attribute@ and their @value@ attribute is an array. |
||
345 | |||
346 | Example: |
||
347 | |||
348 | <pre> |
||
349 | GET /issues/296.json |
||
350 | |||
351 | {"issue": |
||
352 | { |
||
353 | "id":8471, |
||
354 | ... |
||
355 | "custom_fields": |
||
356 | [ |
||
357 | {"value":["1.0.1","1.0.2"],"multiple":true,"name":"Affected version","id":1}, |
||
358 | {"value":"Fixed","name":"Resolution","id":2} |
||
359 | ] |
||
360 | } |
||
361 | } |
||
362 | </pre> |