Rest api » History » Version 90
Jean-Philippe Lang, 2013-01-27 14:57
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 | 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 | 24 | Jean-Philippe Lang | |
28 | 15 | Eric Davis | Status legend: |
29 | 1 | Jean-Philippe Lang | |
30 | * Stable - feature complete, no major changes planned |
||
31 | * Beta - usable for integrations with some bugs or missing minor functionality |
||
32 | * Alpha - major functionality in place, needs feedback from API users and integrators |
||
33 | * Prototype - very rough implementation, possible major breaking changes mid-version. *Not recommended for integration* |
||
34 | * Planned - planned in a future version, depending on developer availability |
||
35 | |||
36 | 24 | Jean-Philippe Lang | h2. General topics |
37 | 1 | Jean-Philippe Lang | |
38 | 78 | Jean-Philippe Lang | h3. Specify @Content-Type@ on @POST@/@PUT@ requests |
39 | 66 | Etienne Massip | |
40 | 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@): |
41 | 82 | Jean-Philippe Lang | * for JSON content, it must be set to @Content-Type: application/json@. |
42 | 78 | Jean-Philippe Lang | * for XML content, to @Content-Type: application/xml@. |
43 | 66 | Etienne Massip | |
44 | 24 | Jean-Philippe Lang | h3. Authentication |
45 | |||
46 | 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: |
||
47 | * using your regular login/password via HTTP Basic authentication. |
||
48 | 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: |
49 | ** passed in as a "key" parameter |
||
50 | ** passed in as a username with a random password via HTTP Basic authentication |
||
51 | 46 | John Galambos | ** passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0) |
52 | 38 | Jean-Philippe Lang | |
53 | You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout. |
||
54 | 24 | Jean-Philippe Lang | |
55 | 89 | Jean-Philippe Lang | h3. User Impersonation |
56 | |||
57 | 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. |
||
58 | |||
59 | 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. |
60 | |||
61 | 24 | Jean-Philippe Lang | h3. Collection resources and pagination |
62 | |||
63 | 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: |
64 | 24 | Jean-Philippe Lang | |
65 | * @offset@: the offset of the first object to retrieve |
||
66 | * @limit@: the number of items to be present in the response (default is 25, maximum is 100) |
||
67 | |||
68 | Examples: |
||
69 | |||
70 | <pre> |
||
71 | GET /issues.xml |
||
72 | => returns the 25 first issues |
||
73 | |||
74 | GET /issues.xml?limit=100 |
||
75 | => returns the 100 first issues |
||
76 | |||
77 | GET /issues.xml?offset=30&limit=10 |
||
78 | => returns 10 issues from the 30th |
||
79 | </pre> |
||
80 | |||
81 | 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: |
||
82 | |||
83 | <pre> |
||
84 | GET /issues.xml |
||
85 | |||
86 | <issues type="array" total_count="2595" limit="25" offset="0"> |
||
87 | ... |
||
88 | </issues> |
||
89 | </pre> |
||
90 | |||
91 | <pre> |
||
92 | GET /issues.json |
||
93 | |||
94 | { "issues":[...], "total_count":2595, "limit":25, "offset":0 } |
||
95 | </pre> |
||
96 | |||
97 | 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: |
||
98 | |||
99 | <pre> |
||
100 | GET /issues.xml?nometa=1 |
||
101 | |||
102 | <issues type="array"> |
||
103 | ... |
||
104 | </issues> |
||
105 | </pre> |
||
106 | 23 | Jean-Philippe Lang | |
107 | 29 | Etienne Massip | h3. Fetching associated data |
108 | |||
109 | 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 : |
||
110 | |||
111 | Example: |
||
112 | |||
113 | 41 | Jean-Philippe Lang | To retrieve issue journals with its description: |
114 | 29 | Etienne Massip | |
115 | <pre> |
||
116 | GET /issues/296.xml?include=journals |
||
117 | |||
118 | <issue> |
||
119 | <id>296</id> |
||
120 | 30 | Etienne Massip | ... |
121 | 29 | Etienne Massip | <journals type="array"> |
122 | ... |
||
123 | 1 | Jean-Philippe Lang | </journals> |
124 | 41 | Jean-Philippe Lang | </issue> |
125 | </pre> |
||
126 | |||
127 | You can also load multiple associations using a coma separated list of items. |
||
128 | |||
129 | Example: |
||
130 | |||
131 | <pre> |
||
132 | GET /issues/296.xml?include=journals,changesets |
||
133 | |||
134 | <issue> |
||
135 | <id>296</id> |
||
136 | ... |
||
137 | <journals type="array"> |
||
138 | ... |
||
139 | </journals> |
||
140 | <changesets type="array"> |
||
141 | ... |
||
142 | </changesets> |
||
143 | 29 | Etienne Massip | </issue> |
144 | </pre> |
||
145 | |||
146 | 42 | Jean-Philippe Lang | h3. Working with custom fields |
147 | |||
148 | Most of the Redmine objects support custom fields. Their values can be found in the @custom_fields@ attributes. |
||
149 | |||
150 | XML Example: |
||
151 | |||
152 | <pre> |
||
153 | GET /issues/296.xml # an issue with 2 custom fields |
||
154 | |||
155 | <issue> |
||
156 | <id>296</id> |
||
157 | ... |
||
158 | <custom_fields type="array"> |
||
159 | <custom_field name="Affected version" id="1"> |
||
160 | <value>1.0.1</value> |
||
161 | </custom_field> |
||
162 | <custom_field name="Resolution" id="2"> |
||
163 | <value>Fixed</value> |
||
164 | </custom_field> |
||
165 | </custom_fields> |
||
166 | </issue> |
||
167 | </pre> |
||
168 | |||
169 | JSON Example: |
||
170 | |||
171 | <pre> |
||
172 | GET /issues/296.json # an issue with 2 custom fields |
||
173 | |||
174 | {"issue": |
||
175 | { |
||
176 | "id":8471, |
||
177 | ... |
||
178 | "custom_fields": |
||
179 | [ |
||
180 | {"value":"1.0.1","name":"Affected version","id":1}, |
||
181 | {"value":"Fixed","name":"Resolution","id":2} |
||
182 | ] |
||
183 | } |
||
184 | } |
||
185 | </pre> |
||
186 | |||
187 | 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). |
||
188 | |||
189 | XML Example: |
||
190 | |||
191 | <pre> |
||
192 | PUT /issues/296.xml |
||
193 | |||
194 | <issue> |
||
195 | <subject>Updating custom fields of an issue</subject> |
||
196 | ... |
||
197 | <custom_fields type="array"> |
||
198 | <custom_field id="1"> |
||
199 | <value>1.0.2</value> |
||
200 | </custom_field> |
||
201 | <custom_field id="2"> |
||
202 | <value>Invalid</value> |
||
203 | </custom_field> |
||
204 | </custom_fields> |
||
205 | </issue> |
||
206 | </pre> |
||
207 | |||
208 | Note: the @type="array"@ attribute on @custom_fields@ XML tag is strictly required. |
||
209 | |||
210 | JSON Example: |
||
211 | |||
212 | <pre> |
||
213 | PUT /issues/296.json |
||
214 | |||
215 | {"issue": |
||
216 | { |
||
217 | "subject":"Updating custom fields of an issue", |
||
218 | ... |
||
219 | "custom_fields": |
||
220 | [ |
||
221 | {"value":"1.0.2","id":1}, |
||
222 | {"value":"Invalid","id":2} |
||
223 | ] |
||
224 | } |
||
225 | } |
||
226 | </pre> |
||
227 | |||
228 | 61 | Jean-Philippe Lang | h3. Attaching files |
229 | |||
230 | Support for adding attachments through the REST API is added in Redmine version:1.4.0. |
||
231 | |||
232 | 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. |
||
233 | |||
234 | <pre> |
||
235 | POST /uploads.xml |
||
236 | Content-Type: application/octet-stream |
||
237 | ... |
||
238 | (request body is the file content) |
||
239 | |||
240 | # 201 response |
||
241 | <upload> |
||
242 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
243 | </upload> |
||
244 | </pre> |
||
245 | |||
246 | Then you can use this token to attach your uploaded file to a new or an existing issue. |
||
247 | |||
248 | <pre> |
||
249 | POST /issues.xml |
||
250 | <issue> |
||
251 | <project_id>1</project_id> |
||
252 | <subject>Creating an issue with a uploaded file</subject> |
||
253 | 62 | Jean-Philippe Lang | <uploads type="array"> |
254 | 61 | Jean-Philippe Lang | <upload> |
255 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
256 | <filename>image.png</filename> |
||
257 | 86 | Etienne Massip | <description>An optional description here</description> |
258 | 61 | Jean-Philippe Lang | <content_type>image/png</content_type> |
259 | </upload> |
||
260 | </uploads> |
||
261 | </issue> |
||
262 | </pre> |
||
263 | |||
264 | 64 | Jean-Philippe Lang | If you try to upload a file that exceeds the maximum size allowed, you get a 422 response: |
265 | |||
266 | <pre> |
||
267 | POST /uploads.xml |
||
268 | Content-Type: application/octet-stream |
||
269 | ... |
||
270 | (request body larger than the maximum size allowed) |
||
271 | |||
272 | # 422 response |
||
273 | <errors> |
||
274 | <error>This file cannot be uploaded because it exceeds the maximum allowed file size (1024000)</error> |
||
275 | </errors> |
||
276 | </pre> |
||
277 | |||
278 | 59 | Jean-Philippe Lang | h3. Validation errors |
279 | |||
280 | 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: |
||
281 | |||
282 | +XML Example+: |
||
283 | |||
284 | <pre> |
||
285 | # Request with invalid or missing attributes |
||
286 | POST /users.xml |
||
287 | <user> |
||
288 | <login>john</login> |
||
289 | <lastname>Smith</lastname> |
||
290 | <mail>john</mail> |
||
291 | </uer> |
||
292 | |||
293 | # 422 response with the error messages in its body |
||
294 | 65 | Jean-Philippe Lang | <errors type="array"> |
295 | 59 | Jean-Philippe Lang | <error>First name can't be blank</error> |
296 | <error>Email is invalid</error> |
||
297 | </errors> |
||
298 | </pre> |
||
299 | |||
300 | |||
301 | +JSON Example+: |
||
302 | |||
303 | <pre> |
||
304 | # Request with invalid or missing attributes |
||
305 | POST /users.json |
||
306 | { |
||
307 | "user":{ |
||
308 | "login":"john", |
||
309 | "lastname":"Smith", |
||
310 | "mail":"john" |
||
311 | } |
||
312 | } |
||
313 | |||
314 | # 422 response with the error messages in its body |
||
315 | { |
||
316 | "errors":[ |
||
317 | "First name can't be blank", |
||
318 | "Email is invalid" |
||
319 | ] |
||
320 | } |
||
321 | </pre> |
||
322 | |||
323 | 81 | Jean-Philippe Lang | h3. JSONP Support |
324 | |||
325 | 88 | Jean-Philippe Lang | 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 -> Authentication. |
326 | 81 | Jean-Philippe Lang | |
327 | Example: |
||
328 | |||
329 | <pre> |
||
330 | GET /issues.json?callback=myHandler |
||
331 | |||
332 | myHandler({"issues":[ ... ]}) |
||
333 | </pre> |
||
334 | |||
335 | 1 | Jean-Philippe Lang | h2. API Usage in various languages/tools |
336 | 5 | Jean-Philippe Lang | |
337 | 1 | Jean-Philippe Lang | * [[Rest_api_with_ruby|Ruby]] |
338 | * [[Rest_api_with_php|PHP]] |
||
339 | 23 | Jean-Philippe Lang | * [[Rest_api_with_python|Python]] |
340 | 27 | Jean-Philippe Lang | * [[Rest_api_with_java|Java]] |
341 | 1 | Jean-Philippe Lang | * [[Rest_api_with_curl|cURL]] |
342 | 37 | Bevan Rudge | * "Drupal Redmine API module, 2.x branch (currently not stable)":http://drupal.org/project/redmine |
343 | 48 | Dorin Huzum | * [[Rest_api_with_csharp|.NET]] |
344 | 49 | Rodrigo Carvalho | * [[Rest_api_with_delphi|Delphi]] |
345 | 54 | Jean-Philippe Lang | |
346 | h2. API Change history |
||
347 | |||
348 | 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]]. |
349 | 57 | Jean-Philippe Lang | |
350 | 54 | Jean-Philippe Lang | h3. 2012-01-29: Multiselect custom fields (r8721, version:1.4.0) |
351 | |||
352 | 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. |
||
353 | |||
354 | Example: |
||
355 | |||
356 | <pre> |
||
357 | GET /issues/296.json |
||
358 | |||
359 | {"issue": |
||
360 | { |
||
361 | "id":8471, |
||
362 | ... |
||
363 | "custom_fields": |
||
364 | [ |
||
365 | {"value":["1.0.1","1.0.2"],"multiple":true,"name":"Affected version","id":1}, |
||
366 | {"value":"Fixed","name":"Resolution","id":2} |
||
367 | ] |
||
368 | } |
||
369 | } |
||
370 | </pre> |