- Snippet visibility level
- List snippets
- Single snippet
- Create new snippet
- Update snippet
- Delete snippet
- Snippet content
- Snippet repository file content
- Get user agent details
Project snippets
Snippet visibility level
Snippets in GitLab can be either private, internal or public.
You can set it with the visibility
field in the snippet.
Constants for snippet visibility levels are:
- Private: The snippet is visible only to project members.
- Internal: The snippet is visible for any authenticated user except external users.
- Public: The snippet can be accessed without any authentication.
Internal
visibility setting is disabled for new projects, groups,
and snippets on GitLab.com. Existing projects, groups, and snippets using the Internal
visibility setting keep this setting. You can read more about the change in the
relevant issue.List snippets
Get a list of project snippets.
GET /projects/:id/snippets
Parameters:
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
Single snippet
Get a single project snippet.
GET /projects/:id/snippets/:snippet_id
Parameters:
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
snippet_id
| integer | yes | The ID of a project’s snippet. |
{
"id": 1,
"title": "test",
"file_name": "add.rb",
"description": "Ruby test snippet",
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"imported": false,
"imported_from": "none",
"project_id": 1,
"web_url": "http://example.com/example/example/snippets/1",
"raw_url": "http://example.com/example/example/snippets/1/raw"
}
Create new snippet
Creates a new project snippet. The user must have permission to create new snippets.
POST /projects/:id/snippets
Parameters:
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
files:content
| string | yes | Content of the snippet file. |
files:file_path
| string | yes | File path of the snippet file. |
title
| string | yes | Title of a snippet. |
content
| string | no | Deprecated: Use files instead. Content of a snippet.
|
description
| string | no | Description of a snippet. |
file_name
| string | no | Deprecated: Use files instead. Name of a snippet file.
|
files
| array of hashes | no | An array of snippet files. |
visibility
| string | no | Snippet’s visibility. |
Example request:
curl --request POST "https://gitlab.com/api/v4/projects/:id/snippets" \
--header "PRIVATE-TOKEN: <your access token>" \
--header "Content-Type: application/json" \
-d @snippet.json
snippet.json
used in the above example request:
{
"title" : "Example Snippet Title",
"description" : "More verbose snippet description",
"visibility" : "private",
"files": [
{
"file_path": "example.txt",
"content" : "source code \n with multiple lines\n"
}
]
}
Update snippet
Updates an existing project snippet. The user must have permission to change an existing snippet.
Updates to snippets with multiple files must use the files
attribute.
PUT /projects/:id/snippets/:snippet_id
Parameters:
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
files:action
| string | yes | Type of action to perform on the file. One of: create , update , delete , move .
|
snippet_id
| integer | yes | The ID of a project’s snippet. |
content
| string | no | Deprecated: Use files instead. Content of a snippet.
|
description
| string | no | Description of a snippet. |
files
| array of hashes | no | An array of snippet files. |
files:content
| string | no | Content of the snippet file. |
files:file_path
| string | no | File path of the snippet file. |
file_name
| string | no | Deprecated: Use files instead. Name of a snippet file.
|
files:previous_path
| string | no | Previous path of the snippet file. |
title
| string | no | Title of a snippet. |
visibility
| string | no | Snippet’s visibility. |
Example request:
curl --request PUT "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id" \
--header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type: application/json" \
-d @snippet.json
snippet.json
used in the above example request:
{
"title" : "Updated Snippet Title",
"description" : "More verbose snippet description",
"visibility" : "private",
"files": [
{
"action": "update",
"file_path": "example.txt",
"content" : "updated source code \n with multiple lines\n"
}
]
}
Delete snippet
Deletes an existing project snippet. This returns a 204 No Content
status code if the operation was successfully or 404
if the resource was not found.
DELETE /projects/:id/snippets/:snippet_id
Parameters:
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
snippet_id
| integer | yes | The ID of a project’s snippet. |
Example request:
curl --request DELETE "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id" \
--header "PRIVATE-TOKEN: <your_access_token>"
Snippet content
Returns the raw project snippet as plain text.
GET /projects/:id/snippets/:snippet_id/raw
Parameters:
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
snippet_id
| integer | yes | The ID of a project’s snippet. |
Example request:
curl "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id/raw" \
--header "PRIVATE-TOKEN: <your_access_token>"
Snippet repository file content
Returns the raw file content as plain text.
GET /projects/:id/snippets/:snippet_id/files/:ref/:file_path/raw
Parameters:
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
file_path
| string | yes | The URL-encoded path to the file, for example, snippet%2Erb .
|
ref
| string | yes | The name of a branch, tag or commit, for example, main .
|
snippet_id
| integer | yes | The ID of a project’s snippet. |
Example request:
curl "https://gitlab.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw" \
--header "PRIVATE-TOKEN: <your_access_token>"
Get user agent details
Available only for users with administrator access.
GET /projects/:id/snippets/:snippet_id/user_agent_detail
Attribute | Type | Required | Description |
---|---|---|---|
id
| integer or string | yes | The ID or URL-encoded path of the project. |
snippet_id
| Integer | yes | The ID of a snippet. |
Example request:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/1/snippets/2/user_agent_detail"
Example response:
{
"user_agent": "AppleWebKit/537.36",
"ip_address": "127.0.0.1",
"akismet_submitted": false
}