Skip to content

Commit 28c14ce

Browse files
committed
Merge branch 'doc_refactor_README_api' into 'master'
Refactor README API documentation ~~Waiting for https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/2435 to be merged.~~ See merge request !2476
2 parents 4ae7cb9 + 84354b4 commit 28c14ce

File tree

1 file changed

+157
-98
lines changed

1 file changed

+157
-98
lines changed

doc/api/README.md

Lines changed: 157 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# GitLab API
22

3+
Automate GitLab via a simple and powerful API. All definitions can be found
4+
under [`/lib/api`](https://gitlab.com/gitlab-org/gitlab-ce/tree/master/lib/api).
5+
36
## Resources
47

8+
Documentation for various API resources can be found separately in the
9+
following locations:
10+
511
- [Users](users.md)
612
- [Session](session.md)
713
- [Projects](projects.md) including setting Webhooks
@@ -27,129 +33,154 @@
2733
- [Build triggers](build_triggers.md)
2834
- [Build Variables](build_variables.md)
2935

30-
## Clients
31-
32-
Find API Clients for GitLab [on our website](https://about.gitlab.com/applications/#api-clients).
33-
You can use [GitLab as an OAuth2 client](oauth2.md) to make API calls.
36+
## Authentication
3437

35-
## Introduction
38+
All API requests require authentication. You need to pass a `private_token`
39+
parameter via query string or header. If passed as a header, the header name
40+
must be `PRIVATE-TOKEN` (uppercase and with a dash instead of an underscore).
41+
You can find or reset your private token in your account page (`/profile/account`).
3642

37-
All API requests require authentication. You need to pass a `private_token` parameter by URL or header. If passed as header, the header name must be "PRIVATE-TOKEN" (capital and with dash instead of underscore). You can find or reset your private token in your profile.
38-
39-
If no, or an invalid, `private_token` is provided then an error message will be returned with status code 401:
43+
If `private_token` is invalid or omitted, then an error message will be
44+
returned with status code `401`:
4045

4146
```json
4247
{
4348
"message": "401 Unauthorized"
4449
}
4550
```
4651

47-
API requests should be prefixed with `api` and the API version. The API version is defined in `lib/api.rb`.
52+
API requests should be prefixed with `api` and the API version. The API version
53+
is defined in [`lib/api.rb`][lib-api-url].
4854

4955
Example of a valid API request:
5056

51-
```
52-
GET http://example.com/api/v3/projects?private_token=QVy1PB7sTxfy4pqfZM1U
57+
```shell
58+
GET https://gitlab.example.com/api/v3/projects?private_token=9koXpg98eAheJpvBs5tK
5359
```
5460

55-
Example for a valid API request using curl and authentication via header:
61+
Example of a valid API request using cURL and authentication via header:
5662

57-
```
58-
curl --header "PRIVATE-TOKEN: QVy1PB7sTxfy4pqfZM1U" "http://example.com/api/v3/projects"
63+
```shell
64+
curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/projects"
5965
```
6066

61-
The API uses JSON to serialize data. You don't need to specify `.json` at the end of API URL.
67+
The API uses JSON to serialize data. You don't need to specify `.json` at the
68+
end of an API URL.
6269

6370
## Authentication with OAuth2 token
6471

65-
Instead of the private_token you can transmit the OAuth2 access token as a header or as a parameter.
72+
Instead of the `private_token` you can transmit the OAuth2 access token as a
73+
header or as a parameter.
6674

67-
### OAuth2 token (as a parameter)
75+
Example of OAuth2 token as a parameter:
6876

69-
```
70-
curl https://localhost:3000/api/v3/user?access_token=OAUTH-TOKEN
77+
```shell
78+
curl https://gitlab.example.com/api/v3/user?access_token=OAUTH-TOKEN
7179
```
7280

73-
### OAuth2 token (as a header)
81+
Example of OAuth2 token as a header:
7482

75-
```
76-
curl -H "Authorization: Bearer OAUTH-TOKEN" https://localhost:3000/api/v3/user
83+
```shell
84+
curl -H "Authorization: Bearer OAUTH-TOKEN" https://example.com/api/v3/user
7785
```
7886

7987
Read more about [GitLab as an OAuth2 client](oauth2.md).
8088

8189
## Status codes
8290

83-
The API is designed to return different status codes according to context and action. In this way if a request results in an error the caller is able to get insight into what went wrong, e.g. status code `400 Bad Request` is returned if a required attribute is missing from the request. The following list gives an overview of how the API functions generally behave.
84-
85-
API request types:
86-
87-
- `GET` requests access one or more resources and return the result as JSON
88-
- `POST` requests return `201 Created` if the resource is successfully created and return the newly created resource as JSON
89-
- `GET`, `PUT` and `DELETE` return `200 OK` if the resource is accessed, modified or deleted successfully, the (modified) result is returned as JSON
90-
- `DELETE` requests are designed to be idempotent, meaning a request a resource still returns `200 OK` even it was deleted before or is not available. The reasoning behind it is the user is not really interested if the resource existed before or not.
91-
92-
The following list shows the possible return codes for API requests.
93-
94-
Return values:
95-
96-
- `200 OK` - The `GET`, `PUT` or `DELETE` request was successful, the resource(s) itself is returned as JSON
97-
- `201 Created` - The `POST` request was successful and the resource is returned as JSON
98-
- `400 Bad Request` - A required attribute of the API request is missing, e.g. the title of an issue is not given
99-
- `401 Unauthorized` - The user is not authenticated, a valid user token is necessary, see above
100-
- `403 Forbidden` - The request is not allowed, e.g. the user is not allowed to delete a project
101-
- `404 Not Found` - A resource could not be accessed, e.g. an ID for a resource could not be found
102-
- `405 Method Not Allowed` - The request is not supported
103-
- `409 Conflict` - A conflicting resource already exists, e.g. creating a project with a name that already exists
104-
- `422 Unprocessable` - The entity could not be processed
105-
- `500 Server Error` - While handling the request something went wrong on the server side
91+
The API is designed to return different status codes according to context and
92+
action. This way, if a request results in an error, the caller is able to get
93+
insight into what went wrong.
94+
95+
The following table gives an overview of how the API functions generally behave.
96+
97+
| Request type | Description |
98+
| ------------ | ----------- |
99+
| `GET` | Access one or more resources and return the result as JSON. |
100+
| `POST` | Return `201 Created` if the resource is successfully created and return the newly created resource as JSON. |
101+
| `GET` / `PUT` / `DELETE` | Return `200 OK` if the resource is accessed, modified or deleted successfully. The (modified) result is returned as JSON. |
102+
| `DELETE` | Designed to be idempotent, meaning a request to a resource still returns `200 OK` even it was deleted before or is not available. The reasoning behind this, is that the user is not really interested if the resource existed before or not. |
103+
104+
The following table shows the possible return codes for API requests.
105+
106+
| Return values | Description |
107+
| ------------- | ----------- |
108+
| `200 OK` | The `GET`, `PUT` or `DELETE` request was successful, the resource(s) itself is returned as JSON. |
109+
| `201 Created` | The `POST` request was successful and the resource is returned as JSON. |
110+
| `400 Bad Request` | A required attribute of the API request is missing, e.g., the title of an issue is not given. |
111+
| `401 Unauthorized` | The user is not authenticated, a valid [user token](#authentication) is necessary. |
112+
| `403 Forbidden` | The request is not allowed, e.g., the user is not allowed to delete a project. |
113+
| `404 Not Found` | A resource could not be accessed, e.g., an ID for a resource could not be found. |
114+
| `405 Method Not Allowed` | The request is not supported. |
115+
| `409 Conflict` | A conflicting resource already exists, e.g., creating a project with a name that already exists. |
116+
| `422 Unprocessable` | The entity could not be processed. |
117+
| `500 Server Error` | While handling the request something went wrong server-side. |
106118

107119
## Sudo
108120

109-
All API requests support performing an api call as if you were another user, if your private token is for an administration account. You need to pass `sudo` parameter by URL or header with an id or username of the user you want to perform the operation as. If passed as header, the header name must be "SUDO" (capitals).
121+
All API requests support performing an API call as if you were another user,
122+
provided your private token is from an administrator account. You need to pass
123+
the `sudo` parameter either via query string or a header with an ID/username of
124+
the user you want to perform the operation as. If passed as a header, the
125+
header name must be `SUDO` (uppercase).
110126

111-
If a non administrative `private_token` is provided then an error message will be returned with status code 403:
127+
If a non administrative `private_token` is provided, then an error message will
128+
be returned with status code `403`:
112129

113130
```json
114131
{
115132
"message": "403 Forbidden: Must be admin to use sudo"
116133
}
117134
```
118135

119-
If the sudo user id or username cannot be found then an error message will be returned with status code 404:
136+
If the sudo user ID or username cannot be found, an error message will be
137+
returned with status code `404`:
120138

121139
```json
122140
{
123141
"message": "404 Not Found: No user id or username for: <id/username>"
124142
}
125143
```
126144

127-
Example of a valid API with sudo request:
145+
---
128146

129-
```
130-
GET http://example.com/api/v3/projects?private_token=QVy1PB7sTxfy4pqfZM1U&sudo=username
131-
```
147+
Example of a valid API call and a request using cURL with sudo request,
148+
providing a username:
132149

150+
```shell
151+
GET /projects?private_token=9koXpg98eAheJpvBs5tK&sudo=username
133152
```
134-
GET http://example.com/api/v3/projects?private_token=QVy1PB7sTxfy4pqfZM1U&sudo=23
153+
154+
```shell
155+
curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --header "SUDO: username" "https://gitlab.example.com/api/v3/projects"
135156
```
136157

137-
Example for a valid API request with sudo using curl and authentication via header:
158+
Example of a valid API call and a request using cURL with sudo request,
159+
providing an ID:
138160

139-
```
140-
curl --header "PRIVATE-TOKEN: QVy1PB7sTxfy4pqfZM1U" --header "SUDO: username" "http://example.com/api/v3/projects"
161+
```shell
162+
GET /projects?private_token=9koXpg98eAheJpvBs5tK&sudo=23
141163
```
142164

143-
```
144-
curl --header "PRIVATE-TOKEN: QVy1PB7sTxfy4pqfZM1U" --header "SUDO: 23" "http://example.com/api/v3/projects"
165+
```shell
166+
curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --header "SUDO: 23" "https://gitlab.example.com/api/v3/projects"
145167
```
146168

147169
## Pagination
148170

149-
When listing resources you can pass the following parameters:
171+
Sometimes the returned result will span across many pages. When listing
172+
resources you can pass the following parameters:
173+
174+
| Parameter | Description |
175+
| --------- | ----------- |
176+
| `page` | Page number (default: `1`) |
177+
| `per_page`| Number of items to list per page (default: `20`, max: `100`) |
150178

151-
- `page` (default: `1`) - page number
152-
- `per_page` (default: `20`, max: `100`) - number of items to list per page
179+
In the example below, we list 50 [namespaces](namespaces.md) per page.
180+
181+
```bash
182+
curl -X PUT -H "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/namespaces?per_page=50
183+
```
153184
154185
### Pagination Link header
155186
@@ -199,65 +230,93 @@ Additional pagination headers are also sent back.
199230
| `X-Next-Page` | The index of the next page |
200231
| `X-Prev-Page` | The index of the previous page |
201232
202-
## id vs iid
233+
## `id` vs `iid`
203234
204-
When you work with API you may notice two similar fields in api entities: id and iid. The main difference between them is scope. Example:
235+
When you work with the API, you may notice two similar fields in API entities:
236+
`id` and `iid`. The main difference between them is scope.
205237
206-
Issue:
238+
For example, an issue might have `id: 46` and `iid: 5`.
207239
208-
id: 46
209-
iid: 5
240+
| Parameter | Description |
241+
| --------- | ----------- |
242+
| `id` | Is unique across all issues and is used for any API call |
243+
| `iid` | Is unique only in scope of a single project. When you browse issues or merge requests with the Web UI, you see the `iid` |
244+
245+
That means that if you want to get an issue via the API you should use the `id`:
246+
247+
```bash
248+
GET /projects/42/issues/:id
249+
```
210250
211-
- id - is unique across all issues. It's used for any api call.
212-
- iid - is unique only in scope of a single project. When you browse issues or merge requests with Web UI, you see iid.
251+
On the other hand, if you want to create a link to a web page you should use
252+
the `iid`:
213253
214-
So if you want to get issue with api you use `http://host/api/v3/.../issues/:id.json`. But when you want to create a link to web page - use `http:://host/project/issues/:iid.json`
254+
```bash
255+
GET /projects/42/issues/:iid
256+
```
215257
216258
## Data validation and error reporting
217259
218-
When working with the API you may encounter validation errors. In such case, the API will answer with an HTTP `400` status.
260+
When working with the API you may encounter validation errors, in which case
261+
the API will answer with an HTTP `400` status.
262+
219263
Such errors appear in two cases:
220264
221-
* A required attribute of the API request is missing, e.g. the title of an issue is not given
222-
* An attribute did not pass the validation, e.g. user bio is too long
265+
- A required attribute of the API request is missing, e.g., the title of an
266+
issue is not given
267+
- An attribute did not pass the validation, e.g., user bio is too long
223268
224269
When an attribute is missing, you will get something like:
225270
226-
HTTP/1.1 400 Bad Request
227-
Content-Type: application/json
228-
229-
{
230-
"message":"400 (Bad request) \"title\" not given"
231-
}
232-
233-
When a validation error occurs, error messages will be different. They will hold all details of validation errors:
271+
```
272+
HTTP/1.1 400 Bad Request
273+
Content-Type: application/json
274+
{
275+
"message":"400 (Bad request) \"title\" not given"
276+
}
277+
```
234278
235-
HTTP/1.1 400 Bad Request
236-
Content-Type: application/json
279+
When a validation error occurs, error messages will be different. They will
280+
hold all details of validation errors:
237281
238-
{
239-
"message": {
240-
"bio": [
241-
"is too long (maximum is 255 characters)"
242-
]
243-
}
282+
```
283+
HTTP/1.1 400 Bad Request
284+
Content-Type: application/json
285+
{
286+
"message": {
287+
"bio": [
288+
"is too long (maximum is 255 characters)"
289+
]
244290
}
291+
}
292+
```
245293
246-
This makes error messages more machine-readable. The format can be described as follow:
294+
This makes error messages more machine-readable. The format can be described as
295+
follows:
247296
248-
{
249-
"message": {
297+
```json
298+
{
299+
"message": {
300+
"<property-name>": [
301+
"<error-message>",
302+
"<error-message>",
303+
...
304+
],
305+
"<embed-entity>": {
250306
"<property-name>": [
251307
"<error-message>",
252308
"<error-message>",
253309
...
254310
],
255-
"<embed-entity>": {
256-
"<property-name>": [
257-
"<error-message>",
258-
"<error-message>",
259-
...
260-
],
261-
}
262311
}
263312
}
313+
}
314+
```
315+
316+
## Clients
317+
318+
There are many unofficial GitLab API Clients for most of the popular
319+
programming languages. Visit the [GitLab website] for a complete list.
320+
321+
[GitLab website]: https://about.gitlab.com/applications/#api-clients "Clients using the GitLab API"
322+
[lib-api-url]: https://gitlab.com/gitlab-org/gitlab-ce/tree/master/lib/api/api.rb

0 commit comments

Comments
 (0)