debian-mirror-gitlab/doc/api/snippets.md

425 lines
12 KiB
Markdown
Raw Normal View History

2020-10-24 23:57:45 +05:30
---
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-09-10 17:25:29 +05:30
# Snippets API
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/6373) in GitLab 8.15.
2019-07-07 11:18:12 +05:30
Snippets API operates on [snippets](../user/snippets.md).
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
## Snippet visibility level
2017-08-17 22:00:37 +05:30
Snippets in GitLab can be either private, internal, or public.
You can set it with the `visibility` field in the snippet.
2019-07-07 11:18:12 +05:30
Valid values for snippet visibility levels are:
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
| 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. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
## List all snippets for a user
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Get a list of the current user's snippets.
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
```plaintext
2019-05-30 16:15:17 +05:30
GET /snippets
2017-08-17 22:00:37 +05:30
```
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2019-05-18 00:54:41 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets"
2019-07-07 11:18:12 +05:30
```
Example response:
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
```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-05-24 23:13:21 +05:30
"web_url": "http://example.com/user0"
2019-07-07 11:18:12 +05:30
},
"updated_at": "2018-09-18T01:12:26.383Z",
"created_at": "2018-09-18T01:12:26.383Z",
"project_id": null,
2020-05-24 23:13:21 +05:30
"web_url": "http://example.com/snippets/42",
"raw_url": "http://example.com/snippets/42/raw"
2019-07-07 11:18:12 +05:30
},
{
"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-05-24 23:13:21 +05:30
"web_url": "http://example.com/user0"
2019-07-07 11:18:12 +05:30
},
"updated_at": "2018-09-18T01:12:26.360Z",
"created_at": "2018-09-18T01:12:26.360Z",
2020-05-24 23:13:21 +05:30
"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-07-07 11:18:12 +05:30
}
]
2019-05-30 16:15:17 +05:30
```
2019-07-07 11:18:12 +05:30
## Get a single snippet
Get a single snippet.
2020-05-24 23:13:21 +05:30
```plaintext
2017-08-17 22:00:37 +05:30
GET /snippets/:id
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------------------|
| `id` | integer | yes | ID of snippet to retrieve. |
Example request:
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
{
"id": 1,
"title": "test",
"file_name": "add.rb",
2017-09-10 17:25:29 +05:30
"description": "Ruby test snippet",
2018-11-08 19:23:39 +05:30
"visibility": "private",
2017-08-17 22:00:37 +05:30
"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-05-24 23:13:21 +05:30
"project_id": null,
2017-08-17 22:00:37 +05:30
"web_url": "http://example.com/snippets/1",
2020-05-24 23:13:21 +05:30
"raw_url": "http://example.com/snippets/1/raw"
2017-08-17 22:00:37 +05:30
}
```
2019-02-15 15:39:39 +05:30
## Single snippet contents
Get a single snippet's raw contents.
2020-05-24 23:13:21 +05:30
```plaintext
2019-02-15 15:39:39 +05:30
GET /snippets/:id/raw
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------------------|
| `id` | integer | yes | ID of snippet to retrieve. |
2019-02-15 15:39:39 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1/raw"
2019-02-15 15:39:39 +05:30
```
Example response:
2020-05-24 23:13:21 +05:30
```plaintext
2019-02-15 15:39:39 +05:30
Hello World snippet
```
2020-07-28 23:09:34 +05:30
## 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-11-24 15:15:51 +05:30
| `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-28 23:09:34 +05:30
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
```
2017-08-17 22:00:37 +05:30
## Create new snippet
2019-07-07 11:18:12 +05:30
Create a new snippet.
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
NOTE: **Note:**
The user must have permission to create new snippets.
2020-05-24 23:13:21 +05:30
```plaintext
2017-08-17 22:00:37 +05:30
POST /snippets
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:--------------|:-------|:---------|:---------------------------------------------------|
| `title` | string | yes | Title of a snippet. |
| `file_name` | string | yes | Name of a snippet file. |
2019-10-12 21:52:04 +05:30
| `content` | string | yes | Content of a snippet. |
2019-07-07 11:18:12 +05:30
| `description` | string | no | Description of a snippet. |
2019-10-12 21:52:04 +05:30
| `visibility` | string | no | Snippet's [visibility](#snippet-visibility-level). |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
```shell
2018-03-17 18:26:18 +05:30
curl --request POST \
2019-10-12 21:52:04 +05:30
--data '{"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
2018-03-17 18:26:18 +05:30
--header 'Content-Type: application/json' \
2020-06-23 00:09:42 +05:30
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/snippets"
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
{
"id": 1,
"title": "This is a snippet",
"file_name": "test.txt",
2017-09-10 17:25:29 +05:30
"description": "Hello World snippet",
2018-11-08 19:23:39 +05:30
"visibility": "internal",
2017-08-17 22:00:37 +05:30
"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-05-24 23:13:21 +05:30
"project_id": null,
2017-08-17 22:00:37 +05:30
"web_url": "http://example.com/snippets/1",
2020-05-24 23:13:21 +05:30
"raw_url": "http://example.com/snippets/1/raw"
2017-08-17 22:00:37 +05:30
}
```
## Update snippet
2019-07-07 11:18:12 +05:30
Update an existing snippet.
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
NOTE: **Note:**
The user must have permission to change an existing snippet.
2020-05-24 23:13:21 +05:30
```plaintext
2017-08-17 22:00:37 +05:30
PUT /snippets/:id
```
Parameters:
2019-07-07 11:18:12 +05:30
| 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-10-12 21:52:04 +05:30
| `content` | string | no | Content of a snippet. |
2019-07-07 11:18:12 +05:30
| `visibility` | string | no | Snippet's [visibility](#snippet-visibility-level). |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
```shell
2018-03-17 18:26:18 +05:30
curl --request PUT \
2019-10-12 21:52:04 +05:30
--data '{"title": "foo", "content": "bar"}' \
2018-03-17 18:26:18 +05:30
--header 'Content-Type: application/json' \
2020-06-23 00:09:42 +05:30
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/snippets/1"
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
{
"id": 1,
"title": "test",
"file_name": "add.rb",
2017-09-10 17:25:29 +05:30
"description": "description of snippet",
2018-11-08 19:23:39 +05:30
"visibility": "internal",
2017-08-17 22:00:37 +05:30
"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-05-24 23:13:21 +05:30
"project_id": null,
2017-08-17 22:00:37 +05:30
"web_url": "http://example.com/snippets/1",
2020-05-24 23:13:21 +05:30
"raw_url": "http://example.com/snippets/1/raw"
2017-08-17 22:00:37 +05:30
}
```
## Delete snippet
2019-07-07 11:18:12 +05:30
Delete an existing snippet.
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
```plaintext
2017-08-17 22:00:37 +05:30
DELETE /snippets/:id
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:-------------------------|
| `id` | integer | yes | ID of snippet to delete. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
```shell
2019-02-15 15:39:39 +05:30
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"
2017-08-17 22:00:37 +05:30
```
2019-07-07 11:18:12 +05:30
The following are possible return codes:
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
| Code | Description |
|:------|:--------------------------------------------|
| `204` | Delete was successful. No data is returned. |
| `404` | The snippet wasn't found. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
## List all public snippets
List all public snippets.
2020-05-24 23:13:21 +05:30
```plaintext
2017-08-17 22:00:37 +05:30
GET /snippets/public
```
2019-07-07 11:18:12 +05:30
Parameters:
| Attribute | Type | Required | Description |
|:-----------|:--------|:---------|:---------------------------------------|
| `per_page` | integer | no | Number of snippets to return per page. |
| `page` | integer | no | Page to retrieve. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=1"
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
[
{
"author": {
"avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
"id": 12,
"name": "Libby Rolfson",
"state": "active",
"username": "elton_wehner",
2020-05-24 23:13:21 +05:30
"web_url": "http://example.com/elton_wehner"
2017-08-17 22:00:37 +05:30
},
"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-05-24 23:13:21 +05:30
"project_id": null,
"web_url": "http://example.com/snippets/49",
"raw_url": "http://example.com/snippets/49/raw"
2017-08-17 22:00:37 +05:30
},
{
"author": {
"avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80&d=identicon",
"id": 16,
"name": "Llewellyn Flatley",
"state": "active",
"username": "adaline",
2020-05-24 23:13:21 +05:30
"web_url": "http://example.com/adaline"
2017-08-17 22:00:37 +05:30
},
"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-05-24 23:13:21 +05:30
"project_id": null,
"web_url": "http://example.com/snippets/48",
"raw_url": "http://example.com/snippets/49/raw",
2018-11-08 19:23:39 +05:30
"visibility": "public"
2017-08-17 22:00:37 +05:30
}
]
```
2017-09-10 17:25:29 +05:30
## Get user agent details
2020-03-13 15:44:24 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/12655) in GitLab 9.4.
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
NOTE: **Note:**
Available only for administrators.
2017-09-10 17:25:29 +05:30
2020-05-24 23:13:21 +05:30
```plaintext
2017-09-10 17:25:29 +05:30
GET /snippets/:id/user_agent_detail
```
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------|
| `id` | integer | yes | ID of snippet. |
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1/user_agent_detail"
2017-09-10 17:25:29 +05:30
```
Example response:
```json
{
"user_agent": "AppleWebKit/537.36",
"ip_address": "127.0.0.1",
"akismet_submitted": false
}
```