2020-07-30 08:09:33 -04:00
---
stage: Create
group: Editor
2020-11-26 01:09:20 -05:00
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/#assignments"
2020-07-30 08:09:33 -04:00
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. |
2020-10-30 11:08:59 -04:00
| `internal` | Snippet is visible for any logged in user except [external users ](../user/permissions.md#external-users ). |
2019-02-08 08:57:31 -05:00
| `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
2020-12-04 16:09:29 -05:00
NOTE:
2019-02-08 08:57:31 -05:00
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:
2020-10-16 14:09:04 -04:00
| Attribute | Type | Required | Description |
|:------------------|:----------------|:---------|:--------------------------------------------------------|
| `title` | string | yes | Title of a snippet |
| `file_name` | string | no | Deprecated: Use `files` instead. Name of a snippet file |
| `content` | string | no | Deprecated: Use `files` instead. Content of a snippet |
| `description` | string | no | Description of a snippet |
| `visibility` | string | no | Snippet's [visibility ](#snippet-visibility-level ) |
| `files` | array of hashes | no | An array of snippet files |
| `files:file_path` | string | yes | File path of the snippet file |
| `files:content` | string | yes | Content of the snippet file |
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
2020-10-16 14:09:04 -04:00
curl --request POST "https://gitlab.example.com/api/v4/snippets" \
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 > " \
2020-10-16 14:09:04 -04:00
-d @snippet .json
```
`snippet.json` used in the above example request:
```json
{
2020-10-20 02:09:03 -04:00
"title": "This is a snippet",
2020-10-16 14:09:04 -04:00
"description": "Hello World snippet",
"visibility": "internal",
"files": [
{
"content": "Hello world",
"file_path": "test.txt"
}
]
}
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",
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-10-16 14:09:04 -04:00
"raw_url": "http://example.com/snippets/1/raw",
"ssh_url_to_repo": "ssh://git@gitlab.example.com:snippets/1.git",
"http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
"file_name": "test.txt",
"files": [
{
"path": "text.txt",
"raw_url": "https://gitlab.example.com/-/snippets/1/raw/master/renamed.md"
}
]
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
2020-12-04 16:09:29 -05:00
NOTE:
2019-02-08 08:57:31 -05:00
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:
2020-10-16 14:09:04 -04:00
| Attribute | Type | Required | Description |
|:----------------------|:----------------|:---------|:------------------------------------------------------------------------------------|
| `id` | integer | yes | ID of snippet to update |
| `title` | string | no | Title of a snippet |
| `file_name` | string | no | Deprecated: Use `files` instead. Name of a snippet file |
| `content` | string | no | Deprecated: Use `files` instead. Content of a snippet |
| `description` | string | no | Description of a snippet |
| `visibility` | string | no | Snippet's [visibility ](#snippet-visibility-level ) |
| `files` | array of hashes | no | An array of snippet files |
| `files:action` | string | yes | Type of action to perform on the file, one of: 'create', 'update', 'delete', 'move' |
| `files:file_path` | string | no | File path of the snippet file |
| `files:previous_path` | string | no | Previous path of the snippet file |
| `files:content` | string | no | Content of the snippet file |
Updates to snippets with multiple files *must* use the `files` attribute.
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
2020-10-16 14:09:04 -04:00
curl --request PUT "https://gitlab.example.com/api/v4/snippets/1" \
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 > " \
2020-10-16 14:09:04 -04:00
-d @snippet .json
```
`snippet.json` used in the above example request:
```json
{
2020-10-20 02:09:03 -04:00
"title": "foo",
2020-10-16 14:09:04 -04:00
"files": [
{
2020-10-20 02:09:03 -04:00
"action": "move",
2020-10-16 14:09:04 -04:00
"previous_path": "test.txt",
"file_path": "renamed.md"
}
]
}
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",
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-10-16 14:09:04 -04:00
"raw_url": "http://example.com/snippets/1/raw",
"ssh_url_to_repo": "ssh://git@gitlab.example.com:snippets/1.git",
"http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
"file_name": "renamed.md",
"files": [
{
"path": "renamed.md",
"raw_url": "https://gitlab.example.com/-/snippets/1/raw/master/renamed.md"
}
]
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
2020-12-04 16:09:29 -05:00
NOTE:
2019-02-08 08:57:31 -05:00
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
}
```