PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/api/README.md

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