Rest api » History » Version 44

Jean-Philippe Lang, 2011-07-06 18:58
Added queries

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 1 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.
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 24 Jean-Philippe Lang
|[[Rest_Issues|Issues]]         | Beta        | Usable with some bugs and rough edges.  | 1.0 |
11 24 Jean-Philippe Lang
|[[Rest_Projects|Projects]]     | Beta        | Usable with some bugs and rough edges.  | 1.0 |
12 28 Jean-Philippe Lang
|[[Rest_Users|Users]]           | Beta | | 1.1 |
13 39 Jean-Philippe Lang
|[[Rest_TimeEntries|Time Entries]]           | Beta | | 1.1 |
14 28 Jean-Philippe Lang
|[[Rest_News|News]]             | Prototype | Prototype implementation for @index@ only | 1.1 |
15 43 Jean-Philippe Lang
|[[Rest_IssueRelations|Issue Relations]]  | Alpha | | 1.3 |
16 43 Jean-Philippe Lang
|[[Rest_Versions|Versions]]  | Alpha | | 1.3 |
17 44 Jean-Philippe Lang
|[[Rest_Queries|Queries]]  | Alpha | | 1.3 |
18 24 Jean-Philippe Lang
19 15 Eric Davis
Status legend:
20 1 Jean-Philippe Lang
21 1 Jean-Philippe Lang
* Stable - feature complete, no major changes planned
22 1 Jean-Philippe Lang
* Beta - usable for integrations with some bugs or missing minor functionality
23 1 Jean-Philippe Lang
* Alpha - major functionality in place, needs feedback from API users and integrators
24 1 Jean-Philippe Lang
* Prototype - very rough implementation, possible major breaking changes mid-version. *Not recommended for integration*
25 1 Jean-Philippe Lang
* Planned - planned in a future version, depending on developer availability
26 1 Jean-Philippe Lang
27 24 Jean-Philippe Lang
h2. General topics
28 1 Jean-Philippe Lang
29 24 Jean-Philippe Lang
h3. Authentication
30 24 Jean-Philippe Lang
31 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:
32 24 Jean-Philippe Lang
* using your regular login/password via HTTP Basic authentication.
33 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:
34 38 Jean-Philippe Lang
** passed in as a "key" parameter
35 38 Jean-Philippe Lang
** passed in as a username with a random password via HTTP Basic authentication
36 38 Jean-Philippe Lang
** passed in as a "Redmine-API-Key" HTTP header (added in Redmine 1.1.0)
37 38 Jean-Philippe Lang
38 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.
39 24 Jean-Philippe Lang
40 24 Jean-Philippe Lang
h3. Collection resources and pagination
41 24 Jean-Philippe Lang
42 24 Jean-Philippe Lang
The response to a GET request on a collection ressources (eg. @/issues.xml@, @/users.xml@) generally won't return all the objets available in your database. Redmine version:1.1.0 introduces a common way to query such ressources using the following parameters:
43 24 Jean-Philippe Lang
44 24 Jean-Philippe Lang
* @offset@: the offset of the first object to retrieve
45 24 Jean-Philippe Lang
* @limit@: the number of items to be present in the response (default is 25, maximum is 100)
46 24 Jean-Philippe Lang
47 25 Jean-Philippe Lang
Alternatively, you can use the @page@ parameter, instead of @offset@, in conjunction with @limit@.
48 24 Jean-Philippe Lang
49 24 Jean-Philippe Lang
Examples:
50 24 Jean-Philippe Lang
51 24 Jean-Philippe Lang
<pre>
52 24 Jean-Philippe Lang
GET /issues.xml
53 24 Jean-Philippe Lang
=> returns the 25 first issues
54 24 Jean-Philippe Lang
55 24 Jean-Philippe Lang
GET /issues.xml?limit=100
56 24 Jean-Philippe Lang
=> returns the 100 first issues
57 24 Jean-Philippe Lang
58 24 Jean-Philippe Lang
GET /issues.xml?offset=30&limit=10
59 24 Jean-Philippe Lang
=> returns 10 issues from the 30th
60 24 Jean-Philippe Lang
61 24 Jean-Philippe Lang
GET /issues.xml?page=3&limit=10
62 24 Jean-Philippe Lang
=> same as above
63 24 Jean-Philippe Lang
</pre>
64 24 Jean-Philippe Lang
65 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:
66 24 Jean-Philippe Lang
67 24 Jean-Philippe Lang
<pre>
68 24 Jean-Philippe Lang
GET /issues.xml
69 24 Jean-Philippe Lang
70 24 Jean-Philippe Lang
<issues type="array" total_count="2595" limit="25" offset="0">
71 24 Jean-Philippe Lang
  ...
72 24 Jean-Philippe Lang
</issues>
73 24 Jean-Philippe Lang
</pre>
74 24 Jean-Philippe Lang
75 24 Jean-Philippe Lang
<pre>
76 24 Jean-Philippe Lang
GET /issues.json
77 24 Jean-Philippe Lang
78 24 Jean-Philippe Lang
{ "issues":[...], "total_count":2595, "limit":25, "offset":0 }
79 24 Jean-Philippe Lang
</pre>
80 24 Jean-Philippe Lang
81 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:
82 24 Jean-Philippe Lang
83 24 Jean-Philippe Lang
<pre>
84 24 Jean-Philippe Lang
GET /issues.xml?nometa=1
85 24 Jean-Philippe Lang
86 24 Jean-Philippe Lang
<issues type="array">
87 24 Jean-Philippe Lang
  ...
88 24 Jean-Philippe Lang
</issues>
89 24 Jean-Philippe Lang
</pre>
90 23 Jean-Philippe Lang
91 29 Etienne Massip
h3. Fetching associated data
92 29 Etienne Massip
93 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 :
94 29 Etienne Massip
95 29 Etienne Massip
Example:
96 29 Etienne Massip
97 41 Jean-Philippe Lang
To retrieve issue journals with its description:
98 29 Etienne Massip
99 29 Etienne Massip
<pre>
100 29 Etienne Massip
GET /issues/296.xml?include=journals
101 29 Etienne Massip
102 29 Etienne Massip
<issue>
103 29 Etienne Massip
  <id>296</id>
104 30 Etienne Massip
  ...
105 29 Etienne Massip
  <journals type="array">
106 29 Etienne Massip
  ...
107 1 Jean-Philippe Lang
  </journals>
108 41 Jean-Philippe Lang
</issue>
109 41 Jean-Philippe Lang
</pre>
110 41 Jean-Philippe Lang
111 41 Jean-Philippe Lang
You can also load multiple associations using a coma separated list of items.
112 41 Jean-Philippe Lang
113 41 Jean-Philippe Lang
Example:
114 41 Jean-Philippe Lang
115 41 Jean-Philippe Lang
<pre>
116 41 Jean-Philippe Lang
GET /issues/296.xml?include=journals,changesets
117 41 Jean-Philippe Lang
118 41 Jean-Philippe Lang
<issue>
119 41 Jean-Philippe Lang
  <id>296</id>
120 41 Jean-Philippe Lang
  ...
121 41 Jean-Philippe Lang
  <journals type="array">
122 41 Jean-Philippe Lang
  ...
123 41 Jean-Philippe Lang
  </journals>
124 41 Jean-Philippe Lang
  <changesets type="array">
125 41 Jean-Philippe Lang
  ...
126 41 Jean-Philippe Lang
  </changesets>
127 29 Etienne Massip
</issue>
128 29 Etienne Massip
</pre>
129 29 Etienne Massip
130 42 Jean-Philippe Lang
h3. Working with custom fields
131 42 Jean-Philippe Lang
132 42 Jean-Philippe Lang
Most of the Redmine objects support custom fields. Their values can be found in the @custom_fields@ attributes.
133 42 Jean-Philippe Lang
134 42 Jean-Philippe Lang
XML Example:
135 42 Jean-Philippe Lang
136 42 Jean-Philippe Lang
<pre>
137 42 Jean-Philippe Lang
GET /issues/296.xml      # an issue with 2 custom fields
138 42 Jean-Philippe Lang
139 42 Jean-Philippe Lang
<issue>
140 42 Jean-Philippe Lang
  <id>296</id>
141 42 Jean-Philippe Lang
  ...
142 42 Jean-Philippe Lang
  <custom_fields type="array">
143 42 Jean-Philippe Lang
    <custom_field name="Affected version" id="1">
144 42 Jean-Philippe Lang
      <value>1.0.1</value>
145 42 Jean-Philippe Lang
    </custom_field>
146 42 Jean-Philippe Lang
    <custom_field name="Resolution" id="2">
147 42 Jean-Philippe Lang
      <value>Fixed</value>
148 42 Jean-Philippe Lang
    </custom_field>
149 42 Jean-Philippe Lang
  </custom_fields>
150 42 Jean-Philippe Lang
</issue>
151 42 Jean-Philippe Lang
</pre>
152 42 Jean-Philippe Lang
153 42 Jean-Philippe Lang
JSON Example:
154 42 Jean-Philippe Lang
155 42 Jean-Philippe Lang
<pre>
156 42 Jean-Philippe Lang
GET /issues/296.json      # an issue with 2 custom fields
157 42 Jean-Philippe Lang
158 42 Jean-Philippe Lang
{"issue":
159 42 Jean-Philippe Lang
  {
160 42 Jean-Philippe Lang
    "id":8471,
161 42 Jean-Philippe Lang
    ...
162 42 Jean-Philippe Lang
    "custom_fields":
163 42 Jean-Philippe Lang
      [
164 42 Jean-Philippe Lang
        {"value":"1.0.1","name":"Affected version","id":1},
165 42 Jean-Philippe Lang
        {"value":"Fixed","name":"Resolution","id":2}
166 42 Jean-Philippe Lang
      ]
167 42 Jean-Philippe Lang
  }
168 42 Jean-Philippe Lang
}
169 42 Jean-Philippe Lang
</pre>
170 42 Jean-Philippe Lang
171 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).
172 42 Jean-Philippe Lang
173 42 Jean-Philippe Lang
XML Example:
174 42 Jean-Philippe Lang
175 42 Jean-Philippe Lang
<pre>
176 42 Jean-Philippe Lang
PUT /issues/296.xml
177 42 Jean-Philippe Lang
178 42 Jean-Philippe Lang
<issue>
179 42 Jean-Philippe Lang
  <subject>Updating custom fields of an issue</subject>
180 42 Jean-Philippe Lang
  ...
181 42 Jean-Philippe Lang
  <custom_fields type="array">
182 42 Jean-Philippe Lang
    <custom_field id="1">
183 42 Jean-Philippe Lang
      <value>1.0.2</value>
184 42 Jean-Philippe Lang
    </custom_field>
185 42 Jean-Philippe Lang
    <custom_field id="2">
186 42 Jean-Philippe Lang
      <value>Invalid</value>
187 42 Jean-Philippe Lang
    </custom_field>
188 42 Jean-Philippe Lang
  </custom_fields>
189 42 Jean-Philippe Lang
</issue>
190 42 Jean-Philippe Lang
</pre>
191 42 Jean-Philippe Lang
192 42 Jean-Philippe Lang
Note: the @type="array"@ attribute on @custom_fields@ XML tag is strictly required.
193 42 Jean-Philippe Lang
194 42 Jean-Philippe Lang
JSON Example:
195 42 Jean-Philippe Lang
196 42 Jean-Philippe Lang
<pre>
197 42 Jean-Philippe Lang
PUT /issues/296.json
198 42 Jean-Philippe Lang
199 42 Jean-Philippe Lang
{"issue":
200 42 Jean-Philippe Lang
  {
201 42 Jean-Philippe Lang
    "subject":"Updating custom fields of an issue",
202 42 Jean-Philippe Lang
    ...
203 42 Jean-Philippe Lang
    "custom_fields":
204 42 Jean-Philippe Lang
      [
205 42 Jean-Philippe Lang
        {"value":"1.0.2","id":1},
206 42 Jean-Philippe Lang
        {"value":"Invalid","id":2}
207 42 Jean-Philippe Lang
      ]
208 42 Jean-Philippe Lang
  }
209 42 Jean-Philippe Lang
}
210 42 Jean-Philippe Lang
</pre>
211 42 Jean-Philippe Lang
212 1 Jean-Philippe Lang
h2. API Usage in various languages/tools
213 5 Jean-Philippe Lang
214 1 Jean-Philippe Lang
* [[Rest_api_with_ruby|Ruby]]
215 1 Jean-Philippe Lang
* [[Rest_api_with_php|PHP]]
216 23 Jean-Philippe Lang
* [[Rest_api_with_python|Python]]
217 27 Jean-Philippe Lang
* [[Rest_api_with_java|Java]]
218 1 Jean-Philippe Lang
* [[Rest_api_with_curl|cURL]]
219 37 Bevan Rudge
* "Drupal Redmine API module, 2.x branch (currently not stable)":http://drupal.org/project/redmine