Rest api » History » Version 79

Jean-Philippe Lang, 2012-06-03 15:01
Groups added

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