Project

General

Profile

Rest api » History » Version 78

Jean-Philippe Lang, 2012-04-11 19:31
formatting

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