Project

General

Profile

Rest api » History » Version 79

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

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 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
* Stable - feature complete, no major changes planned
29
* Beta - usable for integrations with some bugs or missing minor functionality
30
* Alpha - major functionality in place, needs feedback from API users and integrators
31
* Prototype - very rough implementation, possible major breaking changes mid-version. *Not recommended for integration*
32
* Planned - planned in a future version, depending on developer availability
33
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
* for JSON content, it should be set to @Content-Type: application/json@.
40
* for XML content, to @Content-Type: application/xml@.
41 66 Etienne Massip
42 24 Jean-Philippe Lang
h3. Authentication
43
44
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
* 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
** passed in as a "key" parameter
48
** 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
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
h3. Collection resources and pagination
54
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
* @offset@: the offset of the first object to retrieve
58
* @limit@: the number of items to be present in the response (default is 25, maximum is 100)
59
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
Examples:
63
64
<pre>
65
GET /issues.xml
66
=> returns the 25 first issues
67
68
GET /issues.xml?limit=100
69
=> returns the 100 first issues
70
71
GET /issues.xml?offset=30&limit=10
72
=> returns 10 issues from the 30th
73
74
GET /issues.xml?page=3&limit=10
75
=> same as above
76
</pre>
77
78
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
80
<pre>
81
GET /issues.xml
82
83
<issues type="array" total_count="2595" limit="25" offset="0">
84
  ...
85
</issues>
86
</pre>
87
88
<pre>
89
GET /issues.json
90
91
{ "issues":[...], "total_count":2595, "limit":25, "offset":0 }
92
</pre>
93
94
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
96
<pre>
97
GET /issues.xml?nometa=1
98
99
<issues type="array">
100
  ...
101
</issues>
102
</pre>
103 23 Jean-Philippe Lang
104 29 Etienne Massip
h3. Fetching associated data
105
106
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
108
Example:
109
110 41 Jean-Philippe Lang
To retrieve issue journals with its description:
111 29 Etienne Massip
112
<pre>
113
GET /issues/296.xml?include=journals
114
115
<issue>
116
  <id>296</id>
117 30 Etienne Massip
  ...
118 29 Etienne Massip
  <journals type="array">
119
  ...
120 1 Jean-Philippe Lang
  </journals>
121 41 Jean-Philippe Lang
</issue>
122
</pre>
123
124
You can also load multiple associations using a coma separated list of items.
125
126
Example:
127
128
<pre>
129
GET /issues/296.xml?include=journals,changesets
130
131
<issue>
132
  <id>296</id>
133
  ...
134
  <journals type="array">
135
  ...
136
  </journals>
137
  <changesets type="array">
138
  ...
139
  </changesets>
140 29 Etienne Massip
</issue>
141
</pre>
142
143 42 Jean-Philippe Lang
h3. Working with custom fields
144
145
Most of the Redmine objects support custom fields. Their values can be found in the @custom_fields@ attributes.
146
147
XML Example:
148
149
<pre>
150
GET /issues/296.xml      # an issue with 2 custom fields
151
152
<issue>
153
  <id>296</id>
154
  ...
155
  <custom_fields type="array">
156
    <custom_field name="Affected version" id="1">
157
      <value>1.0.1</value>
158
    </custom_field>
159
    <custom_field name="Resolution" id="2">
160
      <value>Fixed</value>
161
    </custom_field>
162
  </custom_fields>
163
</issue>
164
</pre>
165
166
JSON Example:
167
168
<pre>
169
GET /issues/296.json      # an issue with 2 custom fields
170
171
{"issue":
172
  {
173
    "id":8471,
174
    ...
175
    "custom_fields":
176
      [
177
        {"value":"1.0.1","name":"Affected version","id":1},
178
        {"value":"Fixed","name":"Resolution","id":2}
179
      ]
180
  }
181
}
182
</pre>
183
184
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
186
XML Example:
187
188
<pre>
189
PUT /issues/296.xml
190
191
<issue>
192
  <subject>Updating custom fields of an issue</subject>
193
  ...
194
  <custom_fields type="array">
195
    <custom_field id="1">
196
      <value>1.0.2</value>
197
    </custom_field>
198
    <custom_field id="2">
199
      <value>Invalid</value>
200
    </custom_field>
201
  </custom_fields>
202
</issue>
203
</pre>
204
205
Note: the @type="array"@ attribute on @custom_fields@ XML tag is strictly required.
206
207
JSON Example:
208
209
<pre>
210
PUT /issues/296.json
211
212
{"issue":
213
  {
214
    "subject":"Updating custom fields of an issue",
215
    ...
216
    "custom_fields":
217
      [
218
        {"value":"1.0.2","id":1},
219
        {"value":"Invalid","id":2}
220
      ]
221
  }
222
}
223
</pre>
224
225 61 Jean-Philippe Lang
h3. Attaching files
226
227
Support for adding attachments through the REST API is added in Redmine version:1.4.0.
228
229
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
231
<pre>
232
POST /uploads.xml
233
Content-Type: application/octet-stream
234
...
235
(request body is the file content)
236
237
# 201 response
238
<upload>
239
  <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
240
</upload>
241
</pre>
242
243
Then you can use this token to attach your uploaded file to a new or an existing issue.
244
245
<pre>
246
POST /issues.xml
247
<issue>
248
  <project_id>1</project_id>
249
  <subject>Creating an issue with a uploaded file</subject>
250 62 Jean-Philippe Lang
  <uploads type="array">
251 61 Jean-Philippe Lang
    <upload>
252
      <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
253
      <filename>image.png</filename>
254
      <content_type>image/png</content_type>
255
    </upload>
256
  </uploads>
257
</issue>
258
</pre>
259
260 64 Jean-Philippe Lang
If you try to upload a file that exceeds the maximum size allowed, you get a 422 response:
261
262
<pre>
263
POST /uploads.xml
264
Content-Type: application/octet-stream
265
...
266
(request body larger than the maximum size allowed)
267
268
# 422 response
269
<errors>
270
  <error>This file cannot be uploaded because it exceeds the maximum allowed file size (1024000)</error>
271
</errors>
272
</pre>
273
274 59 Jean-Philippe Lang
h3. Validation errors
275
276
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
278
+XML Example+:
279
280
<pre>
281
# Request with invalid or missing attributes
282
POST /users.xml
283
<user>
284
  <login>john</login>
285
  <lastname>Smith</lastname>
286
  <mail>john</mail>
287
</uer>
288
289
# 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
  <error>Email is invalid</error>
293
</errors>
294
</pre>
295
296
297
+JSON Example+:
298
299
<pre>
300
# Request with invalid or missing attributes
301
POST /users.json
302
{
303
  "user":{
304
    "login":"john",
305
    "lastname":"Smith",
306
    "mail":"john"
307
  }
308
}
309
310
# 422 response with the error messages in its body
311
{
312
  "errors":[
313
    "First name can't be blank",
314
    "Email is invalid"
315
  ]
316
}
317
</pre>
318
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
* [[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
h2. API Change history
331
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
336
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
338
Example:
339
340
<pre>
341
GET /issues/296.json
342
343
{"issue":
344
  {
345
    "id":8471,
346
    ...
347
    "custom_fields":
348
      [
349
        {"value":["1.0.1","1.0.2"],"multiple":true,"name":"Affected version","id":1},
350
        {"value":"Fixed","name":"Resolution","id":2}
351
      ]
352
  }
353
}
354
</pre>