2020-07-30 08:09:33 -04:00
---
stage: Create
group: Editor
info: "To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers"
type: reference, api
---
2017-05-10 14:48:07 -04:00
# Snippets API
2016-11-26 10:37:26 -05:00
2020-02-06 10:09:11 -05:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/6373) in GitLab 8.15.
2019-02-08 08:57:31 -05:00
Snippets API operates on [snippets ](../user/snippets.md ).
2016-11-26 10:37:26 -05:00
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
2019-02-08 08:57:31 -05:00
Valid values for snippet visibility levels are:
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
| Visibility | Description |
|:-----------|:----------------------------------------------------|
| `private` | Snippet is visible only to the snippet creator. |
| `internal` | Snippet is visible for any logged in user. |
| `public` | Snippet can be accessed without any authentication. |
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
## List all snippets for a user
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
Get a list of the current user's snippets.
2016-11-26 10:37:26 -05:00
2020-05-19 23:08:04 -04:00
```plaintext
2016-11-26 10:37:26 -05:00
GET /snippets
```
2019-02-08 08:57:31 -05:00
Example request:
2016-11-26 10:37:26 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/snippets"
2019-02-08 08:57:31 -05:00
```
Example response:
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
```json
[
{
"id": 42,
"title": "Voluptatem iure ut qui aut et consequatur quaerat.",
"file_name": "mclaughlin.rb",
"description": null,
"visibility": "internal",
"author": {
"id": 22,
"name": "User 0",
"username": "user0",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80& d=identicon",
2020-04-21 11:21:10 -04:00
"web_url": "http://example.com/user0"
2019-02-08 08:57:31 -05:00
},
"updated_at": "2018-09-18T01:12:26.383Z",
"created_at": "2018-09-18T01:12:26.383Z",
"project_id": null,
2020-04-21 11:21:10 -04:00
"web_url": "http://example.com/snippets/42",
"raw_url": "http://example.com/snippets/42/raw"
2019-02-08 08:57:31 -05:00
},
{
"id": 41,
"title": "Ut praesentium non et atque.",
"file_name": "ondrickaemard.rb",
"description": null,
"visibility": "internal",
"author": {
"id": 22,
"name": "User 0",
"username": "user0",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80& d=identicon",
2020-04-21 11:21:10 -04:00
"web_url": "http://example.com/user0"
2019-02-08 08:57:31 -05:00
},
"updated_at": "2018-09-18T01:12:26.360Z",
"created_at": "2018-09-18T01:12:26.360Z",
2020-04-21 11:21:10 -04:00
"project_id": 1,
"web_url": "http://example.com/gitlab-org/gitlab-test/snippets/41",
"raw_url": "http://example.com/gitlab-org/gitlab-test/snippets/41/raw"
2019-02-08 08:57:31 -05:00
}
]
2016-11-26 10:37:26 -05:00
```
2019-02-08 08:57:31 -05:00
## Get a single snippet
Get a single snippet.
2020-05-19 23:08:04 -04:00
```plaintext
2016-11-26 10:37:26 -05:00
GET /snippets/:id
```
Parameters:
2019-02-08 08:57:31 -05:00
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------------------|
| `id` | integer | yes | ID of snippet to retrieve. |
Example request:
2016-11-26 10:37:26 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04: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",
2020-04-21 11:21:10 -04:00
"project_id": null,
2016-11-26 10:37:26 -05:00
"web_url": "http://example.com/snippets/1",
2020-04-21 11:21:10 -04:00
"raw_url": "http://example.com/snippets/1/raw"
2016-11-26 10:37:26 -05:00
}
```
2018-12-03 22:20:31 -05:00
## Single snippet contents
Get a single snippet's raw contents.
2020-05-19 23:08:04 -04:00
```plaintext
2018-12-03 22:20:31 -05:00
GET /snippets/:id/raw
```
Parameters:
2019-02-08 08:57:31 -05:00
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------------------|
| `id` | integer | yes | ID of snippet to retrieve. |
2018-12-03 22:20:31 -05:00
2019-02-08 08:57:31 -05:00
Example request:
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04: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:
2020-05-19 23:08:04 -04:00
```plaintext
2018-12-03 22:20:31 -05:00
Hello World snippet
```
2020-07-09 08:08:56 -04:00
## Snippet repository file content
Returns the raw file content as plain text.
```plaintext
GET /snippets/:id/files/:ref/:file_path/raw
```
Parameters:
| Attribute | Type | Required | Description |
|:------------|:--------|:---------|:-------------------------------------------------------------------|
2020-09-02 17:10:43 -04:00
| `id` | integer | yes | ID of snippet to retrieve. |
| `ref` | string | yes | Reference to a tag, branch or commit. |
| `file_path` | string | yes | URL-encoded path to the file. |
2020-07-09 08:08:56 -04:00
Example request:
```shell
curl --header "PRIVATE-TOKEN: < your_access_token > " "https://gitlab.example.com/api/v4/snippets/1/files/master/snippet%2Erb/raw"
```
Example response:
```plaintext
Hello World snippet
```
2016-11-26 10:37:26 -05:00
## Create new snippet
2019-02-08 08:57:31 -05:00
Create a new snippet.
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
NOTE: **Note:**
The user must have permission to create new snippets.
2020-05-19 23:08:04 -04:00
```plaintext
2016-11-26 10:37:26 -05:00
POST /snippets
```
Parameters:
2019-02-08 08:57:31 -05:00
| Attribute | Type | Required | Description |
|:--------------|:-------|:---------|:---------------------------------------------------|
| `title` | string | yes | Title of a snippet. |
| `file_name` | string | yes | Name of a snippet file. |
2019-08-27 15:47:00 -04:00
| `content` | string | yes | Content of a snippet. |
2019-02-08 08:57:31 -05:00
| `description` | string | no | Description of a snippet. |
2019-08-27 15:47:00 -04:00
| `visibility` | string | no | Snippet's [visibility ](#snippet-visibility-level ). |
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
Example request:
2016-11-26 10:37:26 -05:00
2020-01-30 10:09:15 -05:00
```shell
2018-01-11 03:04:50 -05:00
curl --request POST \
2019-08-27 15:47:00 -04:00
--data '{"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
2018-01-11 03:04:50 -05:00
--header 'Content-Type: application/json' \
2020-05-27 20:08:37 -04:00
--header "PRIVATE-TOKEN: < your_access_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",
2020-04-21 11:21:10 -04:00
"project_id": null,
2016-11-26 10:37:26 -05:00
"web_url": "http://example.com/snippets/1",
2020-04-21 11:21:10 -04:00
"raw_url": "http://example.com/snippets/1/raw"
2016-11-26 10:37:26 -05:00
}
```
## Update snippet
2019-02-08 08:57:31 -05:00
Update an existing snippet.
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
NOTE: **Note:**
The user must have permission to change an existing snippet.
2020-05-19 23:08:04 -04:00
```plaintext
2016-11-26 10:37:26 -05:00
PUT /snippets/:id
```
Parameters:
2019-02-08 08:57:31 -05:00
| Attribute | Type | Required | Description |
|:--------------|:--------|:---------|:---------------------------------------------------|
| `id` | integer | yes | ID of snippet to update. |
| `title` | string | no | Title of a snippet. |
| `file_name` | string | no | Name of a snippet file. |
| `description` | string | no | Description of a snippet. |
2019-08-27 15:47:00 -04:00
| `content` | string | no | Content of a snippet. |
2019-02-08 08:57:31 -05:00
| `visibility` | string | no | Snippet's [visibility ](#snippet-visibility-level ). |
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
Example request:
2016-11-26 10:37:26 -05:00
2020-01-30 10:09:15 -05:00
```shell
2018-01-11 03:04:50 -05:00
curl --request PUT \
2019-08-27 15:47:00 -04:00
--data '{"title": "foo", "content": "bar"}' \
2018-01-11 03:04:50 -05:00
--header 'Content-Type: application/json' \
2020-05-27 20:08:37 -04:00
--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": "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",
2020-04-21 11:21:10 -04:00
"project_id": null,
2016-11-26 10:37:26 -05:00
"web_url": "http://example.com/snippets/1",
2020-04-21 11:21:10 -04:00
"raw_url": "http://example.com/snippets/1/raw"
2016-11-26 10:37:26 -05:00
}
```
## Delete snippet
2019-02-08 08:57:31 -05:00
Delete an existing snippet.
2016-11-26 10:37:26 -05:00
2020-05-19 23:08:04 -04:00
```plaintext
2016-11-26 10:37:26 -05:00
DELETE /snippets/:id
```
Parameters:
2019-02-08 08:57:31 -05:00
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:-------------------------|
| `id` | integer | yes | ID of snippet to delete. |
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
Example request:
2016-11-26 10:37:26 -05:00
2020-01-30 10:09:15 -05:00
```shell
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
```
2019-02-08 08:57:31 -05:00
The following are possible return codes:
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
| Code | Description |
|:------|:--------------------------------------------|
| `204` | Delete was successful. No data is returned. |
| `404` | The snippet wasn't found. |
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
## List all public snippets
List all public snippets.
2020-05-19 23:08:04 -04:00
```plaintext
2016-11-26 10:37:26 -05:00
GET /snippets/public
```
2019-02-08 08:57:31 -05:00
Parameters:
| Attribute | Type | Required | Description |
|:-----------|:--------|:---------|:---------------------------------------|
| `per_page` | integer | no | Number of snippets to return per page. |
| `page` | integer | no | Page to retrieve. |
2016-11-26 10:37:26 -05:00
2019-02-08 08:57:31 -05:00
Example request:
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04: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",
2020-04-21 11:21:10 -04:00
"web_url": "http://example.com/elton_wehner"
2016-11-26 10:37:26 -05:00
},
"created_at": "2016-11-25T16:53:34.504Z",
"file_name": "oconnerrice.rb",
"id": 49,
"title": "Ratione cupiditate et laborum temporibus.",
"updated_at": "2016-11-25T16:53:34.504Z",
2020-04-21 11:21:10 -04:00
"project_id": null,
"web_url": "http://example.com/snippets/49",
"raw_url": "http://example.com/snippets/49/raw"
2016-11-26 10:37:26 -05:00
},
{
"author": {
"avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80& d=identicon",
"id": 16,
"name": "Llewellyn Flatley",
"state": "active",
"username": "adaline",
2020-04-21 11:21:10 -04:00
"web_url": "http://example.com/adaline"
2016-11-26 10:37:26 -05:00
},
"created_at": "2016-11-25T16:53:34.479Z",
"file_name": "muellershields.rb",
"id": 48,
"title": "Minus similique nesciunt vel fugiat qui ullam sunt.",
"updated_at": "2016-11-25T16:53:34.479Z",
2020-04-21 11:21:10 -04:00
"project_id": null,
"web_url": "http://example.com/snippets/48",
"raw_url": "http://example.com/snippets/49/raw",
2018-06-13 06:28:27 -04:00
"visibility": "public"
2016-11-26 10:37:26 -05:00
}
]
```
2017-07-05 10:50:53 -04:00
## Get user agent details
2020-02-06 10:09:11 -05:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/12655) in GitLab 9.4.
2017-07-07 09:18:01 -04:00
2019-02-08 08:57:31 -05:00
NOTE: **Note:**
Available only for administrators.
2017-07-06 05:48:19 -04:00
2020-05-19 23:08:04 -04:00
```plaintext
2017-07-05 10:50:53 -04:00
GET /snippets/:id/user_agent_detail
```
2019-02-08 08:57:31 -05:00
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------|
| `id` | integer | yes | ID of snippet. |
2017-07-05 10:50:53 -04:00
2019-02-08 08:57:31 -05:00
Example request:
2020-01-30 10:09:15 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --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
}
```