PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/api/README.md

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