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