Rest api » History » Version 101
Go MAEDA, 2019-06-16 11:49
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 | 91 | Jean-Philippe Lang | |[[Rest_Issues|Issues]] | Stable | | 1.0 | |
11 | |[[Rest_Projects|Projects]] | Stable | | 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 | 85 | Jean-Philippe Lang | |[[Rest_WikiPages|Wiki Pages]] | Alpha | | 2.2 | |
19 | 44 | Jean-Philippe Lang | |[[Rest_Queries|Queries]] | Alpha | | 1.3 | |
20 | 63 | Jean-Philippe Lang | |[[Rest_Attachments|Attachments]] | Beta | Adding attachments via the API added in 1.4 | 1.3 | |
21 | 53 | Jean-Philippe Lang | |[[Rest_IssueStatuses|Issue Statuses]] | Alpha | Provides the list of all statuses | 1.3 | |
22 | 51 | Jean-Philippe Lang | |[[Rest_Trackers|Trackers]] | Alpha | Provides the list of all trackers | 1.3 | |
23 | 84 | Jean-Philippe Lang | |[[Rest_Enumerations|Enumerations]] | Alpha | Provides the list of issue priorities and time tracking activities | 2.2 | |
24 | 52 | Jean-Philippe Lang | |[[Rest_IssueCategories|Issue Categories]] | Alpha | | 1.3 | |
25 | 55 | Jean-Philippe Lang | |[[Rest_Roles|Roles]] | Alpha | | 1.4 | |
26 | 79 | Jean-Philippe Lang | |[[Rest_Groups|Groups]] | Alpha | | 2.1 | |
27 | 92 | Jean-Philippe Lang | |[[Rest_CustomFields|Custom Fields]] | Alpha | | 2.4 | |
28 | 95 | Go MAEDA | |[[Rest_Search|Search]] | Alpha | | 3.3 | |
29 | 96 | Go MAEDA | |[[Rest_Files|Files]] | Alpha | | 3.4 | |
30 | 101 | Go MAEDA | |[[Rest_MyAccount|My account]] | Alpha | | 4.1 | |
31 | 24 | Jean-Philippe Lang | |
32 | 15 | Eric Davis | Status legend: |
33 | 1 | Jean-Philippe Lang | |
34 | * Stable - feature complete, no major changes planned |
||
35 | * Beta - usable for integrations with some bugs or missing minor functionality |
||
36 | * Alpha - major functionality in place, needs feedback from API users and integrators |
||
37 | * Prototype - very rough implementation, possible major breaking changes mid-version. *Not recommended for integration* |
||
38 | * Planned - planned in a future version, depending on developer availability |
||
39 | |||
40 | 97 | Jean-Philippe Lang | You can review the list of all the "API changes for each version":/projects/redmine/issues?set_filter=1&status_id=c&fixed_version_id=*&category_id=32&c[]=tracker&c[]=subject&c[]=author&group_by=fixed_version&sort=fixed_version:desc,id. |
41 | |||
42 | 24 | Jean-Philippe Lang | h2. General topics |
43 | 1 | Jean-Philippe Lang | |
44 | 78 | Jean-Philippe Lang | h3. Specify @Content-Type@ on @POST@/@PUT@ requests |
45 | 66 | Etienne Massip | |
46 | 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@): |
47 | 82 | Jean-Philippe Lang | * for JSON content, it must be set to @Content-Type: application/json@. |
48 | 78 | Jean-Philippe Lang | * for XML content, to @Content-Type: application/xml@. |
49 | 66 | Etienne Massip | |
50 | 24 | Jean-Philippe Lang | h3. Authentication |
51 | |||
52 | 99 | Go MAEDA | Most of the time, the API requires authentication. To enable the API-style authentication, you have to check *Enable REST API* in Administration -> Settings -> API. Then, authentication can be done in 2 different ways: |
53 | 24 | Jean-Philippe Lang | * using your regular login/password via HTTP Basic authentication. |
54 | 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: |
55 | ** passed in as a "key" parameter |
||
56 | ** passed in as a username with a random password via HTTP Basic authentication |
||
57 | 46 | John Galambos | ** passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0) |
58 | 38 | Jean-Philippe Lang | |
59 | You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout. |
||
60 | 24 | Jean-Philippe Lang | |
61 | 89 | Jean-Philippe Lang | h3. User Impersonation |
62 | |||
63 | As of Redmine 2.2.0, you can impersonate user through the REST API by setting the @X-Redmine-Switch-User@ header of your API request. It must be set to a user login (eg. @X-Redmine-Switch-User: jsmith@). This only works when using the API with an administrator account, this header will be ignored when using the API with a regular user account. |
||
64 | |||
65 | 90 | Jean-Philippe Lang | If the login specified with the @X-Redmine-Switch-User@ header does not exist or is not active, you will receive a 412 error response. |
66 | |||
67 | 24 | Jean-Philippe Lang | h3. Collection resources and pagination |
68 | |||
69 | 98 | Go MAEDA | The response to a GET request on a collection resources (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 resources using the following parameters: |
70 | 24 | Jean-Philippe Lang | |
71 | * @offset@: the offset of the first object to retrieve |
||
72 | * @limit@: the number of items to be present in the response (default is 25, maximum is 100) |
||
73 | |||
74 | Examples: |
||
75 | |||
76 | <pre> |
||
77 | GET /issues.xml |
||
78 | => returns the 25 first issues |
||
79 | |||
80 | GET /issues.xml?limit=100 |
||
81 | => returns the 100 first issues |
||
82 | |||
83 | GET /issues.xml?offset=30&limit=10 |
||
84 | => returns 10 issues from the 30th |
||
85 | </pre> |
||
86 | |||
87 | 98 | Go MAEDA | Responses to GET requests on collection resources provide information about the total object count available in Redmine and the offset/limit used for the response. Examples: |
88 | 24 | Jean-Philippe Lang | |
89 | <pre> |
||
90 | GET /issues.xml |
||
91 | |||
92 | <issues type="array" total_count="2595" limit="25" offset="0"> |
||
93 | ... |
||
94 | </issues> |
||
95 | </pre> |
||
96 | |||
97 | <pre> |
||
98 | GET /issues.json |
||
99 | |||
100 | { "issues":[...], "total_count":2595, "limit":25, "offset":0 } |
||
101 | </pre> |
||
102 | |||
103 | 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: |
||
104 | |||
105 | <pre> |
||
106 | GET /issues.xml?nometa=1 |
||
107 | |||
108 | <issues type="array"> |
||
109 | ... |
||
110 | </issues> |
||
111 | </pre> |
||
112 | 23 | Jean-Philippe Lang | |
113 | 29 | Etienne Massip | h3. Fetching associated data |
114 | |||
115 | 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 : |
||
116 | |||
117 | Example: |
||
118 | |||
119 | 41 | Jean-Philippe Lang | To retrieve issue journals with its description: |
120 | 29 | Etienne Massip | |
121 | <pre> |
||
122 | GET /issues/296.xml?include=journals |
||
123 | |||
124 | <issue> |
||
125 | <id>296</id> |
||
126 | 30 | Etienne Massip | ... |
127 | 29 | Etienne Massip | <journals type="array"> |
128 | ... |
||
129 | 1 | Jean-Philippe Lang | </journals> |
130 | 41 | Jean-Philippe Lang | </issue> |
131 | </pre> |
||
132 | |||
133 | 98 | Go MAEDA | You can also load multiple associations using a comma separated list of items. |
134 | 41 | Jean-Philippe Lang | |
135 | Example: |
||
136 | |||
137 | <pre> |
||
138 | GET /issues/296.xml?include=journals,changesets |
||
139 | |||
140 | <issue> |
||
141 | <id>296</id> |
||
142 | ... |
||
143 | <journals type="array"> |
||
144 | ... |
||
145 | </journals> |
||
146 | <changesets type="array"> |
||
147 | ... |
||
148 | </changesets> |
||
149 | 29 | Etienne Massip | </issue> |
150 | </pre> |
||
151 | |||
152 | 42 | Jean-Philippe Lang | h3. Working with custom fields |
153 | |||
154 | Most of the Redmine objects support custom fields. Their values can be found in the @custom_fields@ attributes. |
||
155 | |||
156 | XML Example: |
||
157 | |||
158 | <pre> |
||
159 | GET /issues/296.xml # an issue with 2 custom fields |
||
160 | |||
161 | <issue> |
||
162 | <id>296</id> |
||
163 | ... |
||
164 | <custom_fields type="array"> |
||
165 | <custom_field name="Affected version" id="1"> |
||
166 | <value>1.0.1</value> |
||
167 | </custom_field> |
||
168 | <custom_field name="Resolution" id="2"> |
||
169 | <value>Fixed</value> |
||
170 | </custom_field> |
||
171 | </custom_fields> |
||
172 | </issue> |
||
173 | </pre> |
||
174 | |||
175 | JSON Example: |
||
176 | |||
177 | <pre> |
||
178 | GET /issues/296.json # an issue with 2 custom fields |
||
179 | |||
180 | {"issue": |
||
181 | { |
||
182 | "id":8471, |
||
183 | ... |
||
184 | "custom_fields": |
||
185 | [ |
||
186 | {"value":"1.0.1","name":"Affected version","id":1}, |
||
187 | {"value":"Fixed","name":"Resolution","id":2} |
||
188 | ] |
||
189 | } |
||
190 | } |
||
191 | </pre> |
||
192 | |||
193 | 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). |
||
194 | |||
195 | XML Example: |
||
196 | |||
197 | <pre> |
||
198 | PUT /issues/296.xml |
||
199 | |||
200 | <issue> |
||
201 | <subject>Updating custom fields of an issue</subject> |
||
202 | ... |
||
203 | <custom_fields type="array"> |
||
204 | <custom_field id="1"> |
||
205 | <value>1.0.2</value> |
||
206 | </custom_field> |
||
207 | <custom_field id="2"> |
||
208 | <value>Invalid</value> |
||
209 | </custom_field> |
||
210 | </custom_fields> |
||
211 | </issue> |
||
212 | </pre> |
||
213 | |||
214 | Note: the @type="array"@ attribute on @custom_fields@ XML tag is strictly required. |
||
215 | |||
216 | JSON Example: |
||
217 | |||
218 | <pre> |
||
219 | PUT /issues/296.json |
||
220 | |||
221 | {"issue": |
||
222 | { |
||
223 | "subject":"Updating custom fields of an issue", |
||
224 | ... |
||
225 | "custom_fields": |
||
226 | [ |
||
227 | {"value":"1.0.2","id":1}, |
||
228 | {"value":"Invalid","id":2} |
||
229 | ] |
||
230 | } |
||
231 | } |
||
232 | </pre> |
||
233 | |||
234 | 61 | Jean-Philippe Lang | h3. Attaching files |
235 | |||
236 | Support for adding attachments through the REST API is added in Redmine version:1.4.0. |
||
237 | |||
238 | 93 | Jean-Philippe Lang | First, you need to upload each 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. |
239 | 1 | Jean-Philippe Lang | |
240 | 93 | Jean-Philippe Lang | Then you can use this token to attach your uploaded file to a new or an existing issue. |
241 | |||
242 | +XML Example+ |
||
243 | |||
244 | First, upload your file: |
||
245 | |||
246 | 61 | Jean-Philippe Lang | <pre> |
247 | 100 | Go MAEDA | POST /uploads.xml?filename=image.png |
248 | 61 | Jean-Philippe Lang | Content-Type: application/octet-stream |
249 | ... |
||
250 | (request body is the file content) |
||
251 | |||
252 | # 201 response |
||
253 | <upload> |
||
254 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
255 | </upload> |
||
256 | 1 | Jean-Philippe Lang | </pre> |
257 | 61 | Jean-Philippe Lang | |
258 | 93 | Jean-Philippe Lang | Then create the issue using the upload token: |
259 | 61 | Jean-Philippe Lang | |
260 | <pre> |
||
261 | POST /issues.xml |
||
262 | <issue> |
||
263 | <project_id>1</project_id> |
||
264 | <subject>Creating an issue with a uploaded file</subject> |
||
265 | 62 | Jean-Philippe Lang | <uploads type="array"> |
266 | 61 | Jean-Philippe Lang | <upload> |
267 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
268 | <filename>image.png</filename> |
||
269 | 86 | Etienne Massip | <description>An optional description here</description> |
270 | 61 | Jean-Philippe Lang | <content_type>image/png</content_type> |
271 | </upload> |
||
272 | </uploads> |
||
273 | </issue> |
||
274 | </pre> |
||
275 | |||
276 | 64 | Jean-Philippe Lang | If you try to upload a file that exceeds the maximum size allowed, you get a 422 response: |
277 | |||
278 | <pre> |
||
279 | 100 | Go MAEDA | POST /uploads.xml?filename=image.png |
280 | 64 | Jean-Philippe Lang | Content-Type: application/octet-stream |
281 | ... |
||
282 | (request body larger than the maximum size allowed) |
||
283 | |||
284 | # 422 response |
||
285 | <errors> |
||
286 | 1 | Jean-Philippe Lang | <error>This file cannot be uploaded because it exceeds the maximum allowed file size (1024000)</error> |
287 | </errors> |
||
288 | 93 | Jean-Philippe Lang | </pre> |
289 | |||
290 | +JSON Example+ |
||
291 | |||
292 | First, upload your file: |
||
293 | |||
294 | <pre> |
||
295 | 100 | Go MAEDA | POST /uploads.json?filename=image.png |
296 | 93 | Jean-Philippe Lang | Content-Type: application/octet-stream |
297 | ... |
||
298 | (request body is the file content) |
||
299 | |||
300 | # 201 response |
||
301 | {"upload":{"token":"7167.ed1ccdb093229ca1bd0b043618d88743"}} |
||
302 | </pre> |
||
303 | |||
304 | Then create the issue using the upload token: |
||
305 | |||
306 | <pre> |
||
307 | POST /issues.json |
||
308 | { |
||
309 | "issue": { |
||
310 | "project_id": "1", |
||
311 | "subject": "Creating an issue with a uploaded file", |
||
312 | "uploads": [ |
||
313 | {"token": "7167.ed1ccdb093229ca1bd0b043618d88743", "filename": "image.png", "content_type": "image/png"} |
||
314 | ] |
||
315 | } |
||
316 | } |
||
317 | </pre> |
||
318 | |||
319 | You can also upload multiple files (by doing multiple POST requests to @/uploads.json@), then create an issue with multiple attachments: |
||
320 | |||
321 | <pre> |
||
322 | POST /issues.json |
||
323 | { |
||
324 | "issue": { |
||
325 | "project_id": "1", |
||
326 | "subject": "Creating an issue with a uploaded file", |
||
327 | "uploads": [ |
||
328 | {"token": "7167.ed1ccdb093229ca1bd0b043618d88743", "filename": "image1.png", "content_type": "image/png"}, |
||
329 | {"token": "7168.d595398bbb104ed3bba0eed666785cc6", "filename": "image2.png", "content_type": "image/png"} |
||
330 | ] |
||
331 | } |
||
332 | } |
||
333 | 64 | Jean-Philippe Lang | </pre> |
334 | |||
335 | 59 | Jean-Philippe Lang | h3. Validation errors |
336 | |||
337 | 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: |
||
338 | |||
339 | +XML Example+: |
||
340 | |||
341 | <pre> |
||
342 | # Request with invalid or missing attributes |
||
343 | POST /users.xml |
||
344 | <user> |
||
345 | <login>john</login> |
||
346 | <lastname>Smith</lastname> |
||
347 | <mail>john</mail> |
||
348 | </uer> |
||
349 | |||
350 | # 422 response with the error messages in its body |
||
351 | 65 | Jean-Philippe Lang | <errors type="array"> |
352 | 59 | Jean-Philippe Lang | <error>First name can't be blank</error> |
353 | <error>Email is invalid</error> |
||
354 | </errors> |
||
355 | </pre> |
||
356 | |||
357 | |||
358 | +JSON Example+: |
||
359 | |||
360 | <pre> |
||
361 | # Request with invalid or missing attributes |
||
362 | POST /users.json |
||
363 | { |
||
364 | "user":{ |
||
365 | "login":"john", |
||
366 | "lastname":"Smith", |
||
367 | "mail":"john" |
||
368 | } |
||
369 | } |
||
370 | |||
371 | # 422 response with the error messages in its body |
||
372 | { |
||
373 | "errors":[ |
||
374 | "First name can't be blank", |
||
375 | "Email is invalid" |
||
376 | ] |
||
377 | } |
||
378 | </pre> |
||
379 | |||
380 | 81 | Jean-Philippe Lang | h3. JSONP Support |
381 | |||
382 | 99 | Go MAEDA | 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. As of Redmine 2.3.0, JSONP support is optional and disabled by default, you can enable it by checking *Enable JSONP support* in Administration -> Settings -> API. |
383 | 81 | Jean-Philippe Lang | |
384 | Example: |
||
385 | |||
386 | <pre> |
||
387 | GET /issues.json?callback=myHandler |
||
388 | |||
389 | myHandler({"issues":[ ... ]}) |
||
390 | </pre> |
||
391 | |||
392 | 1 | Jean-Philippe Lang | h2. API Usage in various languages/tools |
393 | 5 | Jean-Philippe Lang | |
394 | 1 | Jean-Philippe Lang | * [[Rest_api_with_ruby|Ruby]] |
395 | * [[Rest_api_with_php|PHP]] |
||
396 | 23 | Jean-Philippe Lang | * [[Rest_api_with_python|Python]] |
397 | 94 | Go MAEDA | * [[Rest_api_with_perl|Perl]] |
398 | 27 | Jean-Philippe Lang | * [[Rest_api_with_java|Java]] |
399 | 1 | Jean-Philippe Lang | * [[Rest_api_with_curl|cURL]] |
400 | 37 | Bevan Rudge | * "Drupal Redmine API module, 2.x branch (currently not stable)":http://drupal.org/project/redmine |
401 | 48 | Dorin Huzum | * [[Rest_api_with_csharp|.NET]] |
402 | 49 | Rodrigo Carvalho | * [[Rest_api_with_delphi|Delphi]] |
403 | 54 | Jean-Philippe Lang | |
404 | h2. API Change history |
||
405 | 1 | Jean-Philippe Lang | |
406 | 97 | Jean-Philippe Lang | This section lists changes to the existing API features that may have broken backward compatibility. New features of the API are listed in the [[Rest_api#API-Description|API Description]]. |
407 | 57 | Jean-Philippe Lang | |
408 | 54 | Jean-Philippe Lang | h3. 2012-01-29: Multiselect custom fields (r8721, version:1.4.0) |
409 | |||
410 | 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. |
||
411 | |||
412 | Example: |
||
413 | |||
414 | <pre> |
||
415 | GET /issues/296.json |
||
416 | |||
417 | {"issue": |
||
418 | { |
||
419 | "id":8471, |
||
420 | ... |
||
421 | "custom_fields": |
||
422 | [ |
||
423 | {"value":["1.0.1","1.0.2"],"multiple":true,"name":"Affected version","id":1}, |
||
424 | {"value":"Fixed","name":"Resolution","id":2} |
||
425 | ] |
||
426 | } |
||
427 | } |
||
428 | </pre> |