Project

General

Profile

Rest api » History » Version 102

Go MAEDA, 2022-03-23 15:29

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