2017-05-10 14:48:07 -04:00
|
|
|
# Snippets API
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
> [Introduced][ce-6373] in GitLab 8.15.
|
|
|
|
|
2018-01-11 06:23:42 -05:00
|
|
|
## Snippet visibility level
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
Snippets in GitLab can be either private, internal, or public.
|
2017-02-16 10:42:17 -05:00
|
|
|
You can set it with the `visibility` field in the snippet.
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
Constants for snippet visibility levels are:
|
|
|
|
|
2017-02-16 10:42:17 -05:00
|
|
|
| Visibility | Description |
|
|
|
|
| ---------- | ----------- |
|
|
|
|
| `private` | The snippet is visible only to the snippet creator |
|
|
|
|
| `internal` | The snippet is visible for any logged in user |
|
|
|
|
| `public` | The snippet can be accessed without any authentication |
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
## List snippets
|
|
|
|
|
|
|
|
Get a list of current user's snippets.
|
|
|
|
|
|
|
|
```
|
|
|
|
GET /snippets
|
|
|
|
```
|
|
|
|
|
|
|
|
## Single snippet
|
|
|
|
|
|
|
|
Get a single snippet.
|
|
|
|
|
|
|
|
```
|
|
|
|
GET /snippets/:id
|
|
|
|
```
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
| Attribute | Type | Required | Description |
|
|
|
|
| --------- | ---- | -------- | ----------- |
|
|
|
|
| `id` | Integer | yes | The ID of a snippet |
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```bash
|
2018-12-27 04:03:08 -05:00
|
|
|
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1
|
2016-11-26 10:37:26 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Example response:
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```json
|
2016-11-26 10:37:26 -05:00
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"title": "test",
|
|
|
|
"file_name": "add.rb",
|
2017-05-03 11:26:49 -04:00
|
|
|
"description": "Ruby test snippet",
|
2018-06-13 06:28:27 -04:00
|
|
|
"visibility": "private",
|
2016-11-26 10:37:26 -05:00
|
|
|
"author": {
|
|
|
|
"id": 1,
|
|
|
|
"username": "john_smith",
|
|
|
|
"email": "john@example.com",
|
|
|
|
"name": "John Smith",
|
|
|
|
"state": "active",
|
|
|
|
"created_at": "2012-05-23T08:00:58Z"
|
|
|
|
},
|
|
|
|
"expires_at": null,
|
|
|
|
"updated_at": "2012-06-28T10:52:04Z",
|
|
|
|
"created_at": "2012-06-28T10:52:04Z",
|
|
|
|
"web_url": "http://example.com/snippets/1",
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
## Single snippet contents
|
|
|
|
|
|
|
|
Get a single snippet's raw contents.
|
|
|
|
|
|
|
|
```
|
|
|
|
GET /snippets/:id/raw
|
|
|
|
```
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
| Attribute | Type | Required | Description |
|
|
|
|
| --------- | ---- | -------- | ----------- |
|
|
|
|
| `id` | Integer | yes | The ID of a snippet |
|
|
|
|
|
|
|
|
```bash
|
2018-12-27 04:03:08 -05:00
|
|
|
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/raw
|
2018-12-03 22:20:31 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Example response:
|
|
|
|
|
|
|
|
```
|
|
|
|
Hello World snippet
|
|
|
|
```
|
|
|
|
|
2016-11-26 10:37:26 -05:00
|
|
|
## Create new snippet
|
|
|
|
|
|
|
|
Creates a new snippet. The user must have permission to create new snippets.
|
|
|
|
|
|
|
|
```
|
|
|
|
POST /snippets
|
|
|
|
```
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
| Attribute | Type | Required | Description |
|
|
|
|
| --------- | ---- | -------- | ----------- |
|
|
|
|
| `title` | String | yes | The title of a snippet |
|
|
|
|
| `file_name` | String | yes | The name of a snippet file |
|
|
|
|
| `content` | String | yes | The content of a snippet |
|
|
|
|
| `description` | String | no | The description of a snippet |
|
|
|
|
| `visibility` | String | no | The snippet's visibility |
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```bash
|
2018-01-11 03:04:50 -05:00
|
|
|
curl --request POST \
|
|
|
|
--data '{"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
|
|
|
|
--header 'Content-Type: application/json' \
|
|
|
|
--header "PRIVATE-TOKEN: valid_api_token" \
|
|
|
|
https://gitlab.example.com/api/v4/snippets
|
2016-11-26 10:37:26 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Example response:
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```json
|
2016-11-26 10:37:26 -05:00
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"title": "This is a snippet",
|
|
|
|
"file_name": "test.txt",
|
2017-05-03 11:26:49 -04:00
|
|
|
"description": "Hello World snippet",
|
2018-06-13 06:28:27 -04:00
|
|
|
"visibility": "internal",
|
2016-11-26 10:37:26 -05:00
|
|
|
"author": {
|
|
|
|
"id": 1,
|
|
|
|
"username": "john_smith",
|
|
|
|
"email": "john@example.com",
|
|
|
|
"name": "John Smith",
|
|
|
|
"state": "active",
|
|
|
|
"created_at": "2012-05-23T08:00:58Z"
|
|
|
|
},
|
|
|
|
"expires_at": null,
|
|
|
|
"updated_at": "2012-06-28T10:52:04Z",
|
|
|
|
"created_at": "2012-06-28T10:52:04Z",
|
|
|
|
"web_url": "http://example.com/snippets/1",
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Update snippet
|
|
|
|
|
|
|
|
Updates an existing snippet. The user must have permission to change an existing snippet.
|
|
|
|
|
|
|
|
```
|
|
|
|
PUT /snippets/:id
|
|
|
|
```
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
| Attribute | Type | Required | Description |
|
|
|
|
| --------- | ---- | -------- | ----------- |
|
|
|
|
| `id` | Integer | yes | The ID of a snippet |
|
|
|
|
| `title` | String | no | The title of a snippet |
|
|
|
|
| `file_name` | String | no | The name of a snippet file |
|
|
|
|
| `description` | String | no | The description of a snippet |
|
|
|
|
| `content` | String | no | The content of a snippet |
|
|
|
|
| `visibility` | String | no | The snippet's visibility |
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```bash
|
2018-01-11 03:04:50 -05:00
|
|
|
curl --request PUT \
|
|
|
|
--data '{"title": "foo", "content": "bar"}' \
|
|
|
|
--header 'Content-Type: application/json' \
|
|
|
|
--header "PRIVATE-TOKEN: valid_api_token" \
|
|
|
|
https://gitlab.example.com/api/v4/snippets/1
|
2016-11-26 10:37:26 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Example response:
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```json
|
2016-11-26 10:37:26 -05:00
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"title": "test",
|
|
|
|
"file_name": "add.rb",
|
2017-05-03 11:26:49 -04:00
|
|
|
"description": "description of snippet",
|
2018-06-13 06:28:27 -04:00
|
|
|
"visibility": "internal",
|
2016-11-26 10:37:26 -05:00
|
|
|
"author": {
|
|
|
|
"id": 1,
|
|
|
|
"username": "john_smith",
|
|
|
|
"email": "john@example.com",
|
|
|
|
"name": "John Smith",
|
|
|
|
"state": "active",
|
|
|
|
"created_at": "2012-05-23T08:00:58Z"
|
|
|
|
},
|
|
|
|
"expires_at": null,
|
|
|
|
"updated_at": "2012-06-28T10:52:04Z",
|
|
|
|
"created_at": "2012-06-28T10:52:04Z",
|
|
|
|
"web_url": "http://example.com/snippets/1",
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Delete snippet
|
|
|
|
|
2017-02-16 10:42:17 -05:00
|
|
|
Deletes an existing snippet.
|
2016-11-26 10:37:26 -05:00
|
|
|
|
|
|
|
```
|
|
|
|
DELETE /snippets/:id
|
|
|
|
```
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
| Attribute | Type | Required | Description |
|
|
|
|
| --------- | ---- | -------- | ----------- |
|
|
|
|
| `id` | Integer | yes | The ID of a snippet |
|
|
|
|
|
|
|
|
|
|
|
|
```
|
2018-12-27 04:03:08 -05:00
|
|
|
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"
|
2016-11-26 10:37:26 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
upon successful delete a `204 No content` HTTP code shall be expected, with no data,
|
|
|
|
but if the snippet is non-existent, a `404 Not Found` will be returned.
|
|
|
|
|
|
|
|
## Explore all public snippets
|
|
|
|
|
|
|
|
```
|
|
|
|
GET /snippets/public
|
|
|
|
```
|
|
|
|
|
|
|
|
| Attribute | Type | Required | Description |
|
|
|
|
| --------- | ---- | -------- | ----------- |
|
|
|
|
| `per_page` | Integer | no | number of snippets to return per page |
|
|
|
|
| `page` | Integer | no | the page to retrieve |
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```bash
|
2018-12-27 04:03:08 -05:00
|
|
|
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=1
|
2016-11-26 10:37:26 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Example response:
|
|
|
|
|
2018-12-03 22:20:31 -05:00
|
|
|
```json
|
2016-11-26 10:37:26 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"author": {
|
|
|
|
"avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
|
|
|
|
"id": 12,
|
|
|
|
"name": "Libby Rolfson",
|
|
|
|
"state": "active",
|
|
|
|
"username": "elton_wehner",
|
|
|
|
"web_url": "http://localhost:3000/elton_wehner"
|
|
|
|
},
|
|
|
|
"created_at": "2016-11-25T16:53:34.504Z",
|
|
|
|
"file_name": "oconnerrice.rb",
|
|
|
|
"id": 49,
|
|
|
|
"raw_url": "http://localhost:3000/snippets/49/raw",
|
|
|
|
"title": "Ratione cupiditate et laborum temporibus.",
|
|
|
|
"updated_at": "2016-11-25T16:53:34.504Z",
|
|
|
|
"web_url": "http://localhost:3000/snippets/49"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"author": {
|
|
|
|
"avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80&d=identicon",
|
|
|
|
"id": 16,
|
|
|
|
"name": "Llewellyn Flatley",
|
|
|
|
"state": "active",
|
|
|
|
"username": "adaline",
|
|
|
|
"web_url": "http://localhost:3000/adaline"
|
|
|
|
},
|
|
|
|
"created_at": "2016-11-25T16:53:34.479Z",
|
|
|
|
"file_name": "muellershields.rb",
|
|
|
|
"id": 48,
|
|
|
|
"raw_url": "http://localhost:3000/snippets/48/raw",
|
|
|
|
"title": "Minus similique nesciunt vel fugiat qui ullam sunt.",
|
|
|
|
"updated_at": "2016-11-25T16:53:34.479Z",
|
2018-06-13 06:28:27 -04:00
|
|
|
"web_url": "http://localhost:3000/snippets/48",
|
|
|
|
"visibility": "public"
|
2016-11-26 10:37:26 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
2017-07-05 10:50:53 -04:00
|
|
|
|
|
|
|
## Get user agent details
|
|
|
|
|
2017-07-07 09:18:01 -04:00
|
|
|
> **Notes:**
|
|
|
|
> [Introduced][ce-29508] in GitLab 9.4.
|
|
|
|
|
|
|
|
|
2017-07-06 05:48:19 -04:00
|
|
|
Available only for admins.
|
|
|
|
|
2017-07-05 10:50:53 -04:00
|
|
|
```
|
|
|
|
GET /snippets/:id/user_agent_detail
|
|
|
|
```
|
|
|
|
|
|
|
|
| Attribute | Type | Required | Description |
|
|
|
|
|-------------|---------|----------|--------------------------------------|
|
|
|
|
| `id` | Integer | yes | The ID of a snippet |
|
|
|
|
|
|
|
|
```bash
|
2018-12-27 04:03:08 -05:00
|
|
|
curl --request GET --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/user_agent_detail
|
2017-07-05 10:50:53 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Example response:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"user_agent": "AppleWebKit/537.36",
|
|
|
|
"ip_address": "127.0.0.1",
|
2017-07-06 09:19:14 -04:00
|
|
|
"akismet_submitted": false
|
2017-07-05 10:50:53 -04:00
|
|
|
}
|
|
|
|
```
|
2017-07-07 09:54:39 -04:00
|
|
|
|
2018-01-11 06:23:42 -05:00
|
|
|
[ce-6373]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6373
|
|
|
|
[ce-29508]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/12655
|