PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/api/README.md

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