Project

General

Profile

Rest api » History » Version 95

Go MAEDA, 2016-03-20 03:28
Added Search API

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