PageRenderTime 38ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/api/README.md

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