PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/api/README.md

https://gitlab.com/toanalien/gitlab-ee
Markdown | 326 lines | 251 code | 75 blank | 0 comment | 0 complexity | 91d2f397e30d37dfa27be8831ccef17d MD5 | raw file
  1. # GitLab API
  2. Automate GitLab via a simple and powerful API. All definitions can be found
  3. under [`/lib/api`](https://gitlab.com/gitlab-org/gitlab-ce/tree/master/lib/api).
  4. ## Resources
  5. Documentation for various API resources can be found separately in the
  6. following locations:
  7. - [Users](users.md)
  8. - [Session](session.md)
  9. - [Projects](projects.md) including setting Webhooks
  10. - [Project Snippets](project_snippets.md)
  11. - [Services](services.md)
  12. - [Repositories](repositories.md)
  13. - [Repository Files](repository_files.md)
  14. - [Commits](commits.md)
  15. - [Tags](tags.md)
  16. - [Branches](branches.md)
  17. - [Merge Requests](merge_requests.md)
  18. - [Issues](issues.md)
  19. - [Labels](labels.md)
  20. - [Milestones](milestones.md)
  21. - [Notes](notes.md) (comments)
  22. - [Deploy Keys](deploy_keys.md)
  23. - [System Hooks](system_hooks.md)
  24. - [Groups](groups.md)
  25. - [Namespaces](namespaces.md)
  26. - [Settings](settings.md)
  27. - [Keys](keys.md)
  28. - [Builds](builds.md)
  29. - [Build triggers](build_triggers.md)
  30. - [Build Variables](build_variables.md)
  31. - [Runners](runners.md)
  32. - [Open source license templates](licenses.md)
  33. - [License](license.md)
  34. ## Authentication
  35. All API requests require authentication. You need to pass a `private_token`
  36. parameter via query string or header. If passed as a header, the header name
  37. must be `PRIVATE-TOKEN` (uppercase and with a dash instead of an underscore).
  38. You can find or reset your private token in your account page (`/profile/account`).
  39. If `private_token` is invalid or omitted, then an error message will be
  40. returned with status code `401`:
  41. ```json
  42. {
  43. "message": "401 Unauthorized"
  44. }
  45. ```
  46. API requests should be prefixed with `api` and the API version. The API version
  47. is defined in [`lib/api.rb`][lib-api-url].
  48. Example of a valid API request:
  49. ```shell
  50. GET https://gitlab.example.com/api/v3/projects?private_token=9koXpg98eAheJpvBs5tK
  51. ```
  52. Example of a valid API request using cURL and authentication via header:
  53. ```shell
  54. curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/projects"
  55. ```
  56. The API uses JSON to serialize data. You don't need to specify `.json` at the
  57. end of an API URL.
  58. ## Authentication with OAuth2 token
  59. Instead of the `private_token` you can transmit the OAuth2 access token as a
  60. header or as a parameter.
  61. Example of OAuth2 token as a parameter:
  62. ```shell
  63. curl https://gitlab.example.com/api/v3/user?access_token=OAUTH-TOKEN
  64. ```
  65. Example of OAuth2 token as a header:
  66. ```shell
  67. curl -H "Authorization: Bearer OAUTH-TOKEN" https://example.com/api/v3/user
  68. ```
  69. Read more about [GitLab as an OAuth2 client](oauth2.md).
  70. ## Status codes
  71. The API is designed to return different status codes according to context and
  72. action. This way, if a request results in an error, the caller is able to get
  73. insight into what went wrong.
  74. The following table gives an overview of how the API functions generally behave.
  75. | Request type | Description |
  76. | ------------ | ----------- |
  77. | `GET` | Access one or more resources and return the result as JSON. |
  78. | `POST` | Return `201 Created` if the resource is successfully created and return the newly created resource as JSON. |
  79. | `GET` / `PUT` / `DELETE` | Return `200 OK` if the resource is accessed, modified or deleted successfully. The (modified) result is returned as JSON. |
  80. | `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. |
  81. The following table shows the possible return codes for API requests.
  82. | Return values | Description |
  83. | ------------- | ----------- |
  84. | `200 OK` | The `GET`, `PUT` or `DELETE` request was successful, the resource(s) itself is returned as JSON. |
  85. | `201 Created` | The `POST` request was successful and the resource is returned as JSON. |
  86. | `304 Not Modified` | Indicates that the resource has not been modified since the last request. |
  87. | `400 Bad Request` | A required attribute of the API request is missing, e.g., the title of an issue is not given. |
  88. | `401 Unauthorized` | The user is not authenticated, a valid [user token](#authentication) is necessary. |
  89. | `403 Forbidden` | The request is not allowed, e.g., the user is not allowed to delete a project. |
  90. | `404 Not Found` | A resource could not be accessed, e.g., an ID for a resource could not be found. |
  91. | `405 Method Not Allowed` | The request is not supported. |
  92. | `409 Conflict` | A conflicting resource already exists, e.g., creating a project with a name that already exists. |
  93. | `422 Unprocessable` | The entity could not be processed. |
  94. | `500 Server Error` | While handling the request something went wrong server-side. |
  95. ## Sudo
  96. All API requests support performing an API call as if you were another user,
  97. provided your private token is from an administrator account. You need to pass
  98. the `sudo` parameter either via query string or a header with an ID/username of
  99. the user you want to perform the operation as. If passed as a header, the
  100. header name must be `SUDO` (uppercase).
  101. If a non administrative `private_token` is provided, then an error message will
  102. be returned with status code `403`:
  103. ```json
  104. {
  105. "message": "403 Forbidden: Must be admin to use sudo"
  106. }
  107. ```
  108. If the sudo user ID or username cannot be found, an error message will be
  109. returned with status code `404`:
  110. ```json
  111. {
  112. "message": "404 Not Found: No user id or username for: <id/username>"
  113. }
  114. ```
  115. ---
  116. Example of a valid API call and a request using cURL with sudo request,
  117. providing a username:
  118. ```shell
  119. GET /projects?private_token=9koXpg98eAheJpvBs5tK&sudo=username
  120. ```
  121. ```shell
  122. curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --header "SUDO: username" "https://gitlab.example.com/api/v3/projects"
  123. ```
  124. Example of a valid API call and a request using cURL with sudo request,
  125. providing an ID:
  126. ```shell
  127. GET /projects?private_token=9koXpg98eAheJpvBs5tK&sudo=23
  128. ```
  129. ```shell
  130. curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --header "SUDO: 23" "https://gitlab.example.com/api/v3/projects"
  131. ```
  132. ## Pagination
  133. Sometimes the returned result will span across many pages. When listing
  134. resources you can pass the following parameters:
  135. | Parameter | Description |
  136. | --------- | ----------- |
  137. | `page` | Page number (default: `1`) |
  138. | `per_page`| Number of items to list per page (default: `20`, max: `100`) |
  139. In the example below, we list 50 [namespaces](namespaces.md) per page.
  140. ```bash
  141. curl -X PUT -H "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/namespaces?per_page=50
  142. ```
  143. ### Pagination Link header
  144. [Link headers](http://www.w3.org/wiki/LinkHeader) are sent back with each
  145. response. They have `rel` set to prev/next/first/last and contain the relevant
  146. URL. Please use these links instead of generating your own URLs.
  147. In the cURL example below, we limit the output to 3 items per page (`per_page=3`)
  148. and we request the second page (`page=2`) of [comments](notes.md) of the issue
  149. with ID `8` which belongs to the project with ID `8`:
  150. ```bash
  151. curl -I -H "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab.example.com/api/v3/projects/8/issues/8/notes?per_page=3&page=2
  152. ```
  153. The response will then be:
  154. ```
  155. HTTP/1.1 200 OK
  156. Cache-Control: no-cache
  157. Content-Length: 1103
  158. Content-Type: application/json
  159. Date: Mon, 18 Jan 2016 09:43:18 GMT
  160. Link: <https://gitlab.example.com/api/v3/projects/8/issues/8/notes?page=1&per_page=3>; rel="prev", <https://gitlab.example.com/api/v3/projects/8/issues/8/notes?page=3&per_page=3>; rel="next", <https://gitlab.example.com/api/v3/projects/8/issues/8/notes?page=1&per_page=3>; rel="first", <https://gitlab.example.com/api/v3/projects/8/issues/8/notes?page=3&per_page=3>; rel="last"
  161. Status: 200 OK
  162. Vary: Origin
  163. X-Next-Page: 3
  164. X-Page: 2
  165. X-Per-Page: 3
  166. X-Prev-Page: 1
  167. X-Request-Id: 732ad4ee-9870-4866-a199-a9db0cde3c86
  168. X-Runtime: 0.108688
  169. X-Total: 8
  170. X-Total-Pages: 3
  171. ```
  172. ### Other pagination headers
  173. Additional pagination headers are also sent back.
  174. | Header | Description |
  175. | ------ | ----------- |
  176. | `X-Total` | The total number of items |
  177. | `X-Total-Pages` | The total number of pages |
  178. | `X-Per-Page` | The number of items per page |
  179. | `X-Page` | The index of the current page (starting at 1) |
  180. | `X-Next-Page` | The index of the next page |
  181. | `X-Prev-Page` | The index of the previous page |
  182. ## `id` vs `iid`
  183. When you work with the API, you may notice two similar fields in API entities:
  184. `id` and `iid`. The main difference between them is scope.
  185. For example, an issue might have `id: 46` and `iid: 5`.
  186. | Parameter | Description |
  187. | --------- | ----------- |
  188. | `id` | Is unique across all issues and is used for any API call |
  189. | `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` |
  190. That means that if you want to get an issue via the API you should use the `id`:
  191. ```bash
  192. GET /projects/42/issues/:id
  193. ```
  194. On the other hand, if you want to create a link to a web page you should use
  195. the `iid`:
  196. ```bash
  197. GET /projects/42/issues/:iid
  198. ```
  199. ## Data validation and error reporting
  200. When working with the API you may encounter validation errors, in which case
  201. the API will answer with an HTTP `400` status.
  202. Such errors appear in two cases:
  203. - A required attribute of the API request is missing, e.g., the title of an
  204. issue is not given
  205. - An attribute did not pass the validation, e.g., user bio is too long
  206. When an attribute is missing, you will get something like:
  207. ```
  208. HTTP/1.1 400 Bad Request
  209. Content-Type: application/json
  210. {
  211. "message":"400 (Bad request) \"title\" not given"
  212. }
  213. ```
  214. When a validation error occurs, error messages will be different. They will
  215. hold all details of validation errors:
  216. ```
  217. HTTP/1.1 400 Bad Request
  218. Content-Type: application/json
  219. {
  220. "message": {
  221. "bio": [
  222. "is too long (maximum is 255 characters)"
  223. ]
  224. }
  225. }
  226. ```
  227. This makes error messages more machine-readable. The format can be described as
  228. follows:
  229. ```json
  230. {
  231. "message": {
  232. "<property-name>": [
  233. "<error-message>",
  234. "<error-message>",
  235. ...
  236. ],
  237. "<embed-entity>": {
  238. "<property-name>": [
  239. "<error-message>",
  240. "<error-message>",
  241. ...
  242. ],
  243. }
  244. }
  245. }
  246. ```
  247. ## Clients
  248. There are many unofficial GitLab API Clients for most of the popular
  249. programming languages. Visit the [GitLab website] for a complete list.
  250. [GitLab website]: https://about.gitlab.com/applications/#api-clients "Clients using the GitLab API"
  251. [lib-api-url]: https://gitlab.com/gitlab-org/gitlab-ce/tree/master/lib/api/api.rb