Use this API to manage personal and project snippets.

To move snippets between storages, use the snippet repository storage moves API.

List all snippets for a project

GET /api/v4/projects/{id}/snippets

Lists all snippets for a specified project.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
page
Query
IntegerPage number of results to retrieve
Default: 1
Example: 1
per_page
Query
IntegerNumber of items to list per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKArray of APIEntitiesProjectSnippet
400Bad Request—
404Not found—

Create a project snippet

POST /api/v4/projects/{id}/snippets

Creates a project snippet. The user must have permission to create snippets.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project

Request body (application/json)

PropertyTypeDescription
contentStringThe content of a snippet. Mutually exclusive with files. Exactly one of files, content must be provided
descriptionStringDescription of the snippet
file_name
Required
StringThe name of a snippet file
filesArray of objectsArray of files for the snippet. Mutually exclusive with content. Exactly one of files, content must be provided
files[].content
Required
StringContent of the snippet file
files[].file_path
Required
StringPath of the snippet file
title
Required
StringTitle of the snippet
visibility
Required
StringVisibility level for the snippet. On GitLab.com, the internal value is not available
Allowed values: private, internal, public
Minimum length: 1

Responses

CodeDescriptionSchema
201CreatedAPIEntitiesProjectSnippet
400Validation error—
404Not found—
422Unprocessable entity—

Retrieve a project snippet

GET /api/v4/projects/{id}/snippets/{snippet_id}

Retrieves a specified project snippet.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
snippet_id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesProjectSnippet
400Bad Request—
404Not found—

Update a project snippet

PUT /api/v4/projects/{id}/snippets/{snippet_id}

Updates a specified project snippet. The user must have permission to modify snippets. Updates to snippets with multiple files must use the files attribute.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
snippet_id
Path, required
IntegerID of the snippet

Request body (application/json)

PropertyTypeDescription
contentStringContent of the snippet. Deprecated. Use files instead. Mutually exclusive with files. At least one of content, description, files, file_name, title, visibility must be provided
descriptionStringDescription of the snippet. At least one of content, description, files, file_name, title, visibility must be provided
file_nameStringName of the snippet file. Deprecated. Use files instead. Mutually exclusive with files. At least one of content, description, files, file_name, title, visibility must be provided
filesArray of objectsArray of files to update for the snippet. Required when the snippet has multiple files. Mutually exclusive with content, file_name. At least one of content, description, files, file_name, title, visibility must be provided
files[].action
Required
StringThe type of action to perform on the file, must be one of: create, update, delete, move
Allowed values: create, update, delete, move
Minimum length: 1
files[].contentStringContent of the snippet file
files[].file_pathStringPath of the snippet file
files[].previous_pathStringPrevious path of the snippet file
titleStringTitle of the snippet. At least one of content, description, files, file_name, title, visibility must be provided
visibilityStringVisibility level for the snippet. On GitLab.com, the internal value is not available. At least one of content, description, files, file_name, title, visibility must be provided
Allowed values: private, internal, public

Responses

CodeDescriptionSchema
200OKAPIEntitiesProjectSnippet
400Validation error—
404Not found—
422Unprocessable entity—

Delete a project snippet

DELETE /api/v4/projects/{id}/snippets/{snippet_id}

Deletes a specified project snippet.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
snippet_id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
204No Content—
400Validation error—
404Not found—

Retrieve snippet repository file content

GET /api/v4/projects/{id}/snippets/{snippet_id}/files/{ref}/{file_path}/raw

Retrieves the raw file content from a snippet repository as plain text.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
ref
Path, required
StringThe name of branch, tag or commit
file_path
Path, required
StringThe URL-encoded path to the file, like lib%2Fclass%2Erb
snippet_id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesProjectSnippet
400Bad Request—
404Not found—

Retrieve a raw project snippet

GET /api/v4/projects/{id}/snippets/{snippet_id}/raw

Retrieves the raw contents of a specified project snippet as plain text

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
snippet_id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesProjectSnippet
400Bad Request—
404Not found—

Retrieve user agent details for a project snippet

GET /api/v4/projects/{id}/snippets/{snippet_id}/user_agent_detail

Retrieves user agent details for a specified snippet. Available only for administrators.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
snippet_id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesUserAgentDetail
400Bad Request—
404Not found—

List all snippets for current user

GET /api/v4/snippets

Lists all snippets for the currently authenticated user.

Parameters

NameTypeDescription
created_after
Query
String (date-time)Return snippets created on or after the specified time
created_before
Query
String (date-time)Return snippets created on or before the specified time
page
Query
IntegerPage number of results to retrieve
Default: 1
Example: 1
per_page
Query
IntegerNumber of items to list per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKArray of APIEntitiesSnippet
400Bad Request—
404Not found—

Create a snippet

POST /api/v4/snippets

Creates a snippet.

Request body (application/json)

PropertyTypeDescription
contentStringThe content of a snippet. Mutually exclusive with files. Exactly one of files, content must be provided
descriptionStringDescription of the snippet
file_name
Required
StringThe name of a snippet file
filesArray of objectsArray of files for the snippet. Mutually exclusive with content. Exactly one of files, content must be provided
files[].content
Required
StringContent of the snippet file
files[].file_path
Required
StringPath of the snippet file
title
Required
StringTitle of the snippet
visibilityStringVisibility level for the snippet. On GitLab.com, the internal value is not available
Allowed values: private, internal, public
Default: internal

Responses

CodeDescriptionSchema
201CreatedAPIEntitiesPersonalSnippet
400Validation error—
404Not found—
422Unprocessable entity—

List all snippets

GET /api/v4/snippets/all

Lists all snippets available to the currently authenticated user. Users with Administrator or Auditor access levels can see all snippets (both personal and project). This feature was introduced in GitLab 16.3.

Parameters

NameTypeDescription
created_after
Query
String (date-time)Return snippets created on or after the specified time
created_before
Query
String (date-time)Return snippets created on or before the specified time
page
Query
IntegerPage number of results to retrieve
Default: 1
Example: 1
per_page
Query
IntegerNumber of items to list per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKArray of APIEntitiesSnippet
400Bad Request—
404Not found—

List all public snippets

GET /api/v4/snippets/public

Lists all public snippets accessible to the currently authenticated user.

Parameters

NameTypeDescription
created_after
Query
String (date-time)Return snippets created on or after the specified time
created_before
Query
String (date-time)Return snippets created on or before the specified time
page
Query
IntegerPage number of results to retrieve
Default: 1
Example: 1
per_page
Query
IntegerNumber of items to list per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKArray of APIEntitiesPersonalSnippet
400Bad Request—
404Not found—

Retrieve a snippet

GET /api/v4/snippets/{id}

Retrieves a specified snippet.

Parameters

NameTypeDescription
id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesPersonalSnippet
400Bad Request—
404Not found—

Update snippet

PUT /api/v4/snippets/{id}

Updates a specified snippet.

Parameters

NameTypeDescription
id
Path, required
IntegerID of the snippet

Request body (application/json)

PropertyTypeDescription
contentStringThe content of a snippet. Mutually exclusive with files. At least one of content, description, files, file_name, title, visibility must be provided
descriptionStringDescription of the snippet. At least one of content, description, files, file_name, title, visibility must be provided
file_nameStringThe name of a snippet file. Mutually exclusive with files. At least one of content, description, files, file_name, title, visibility must be provided
filesArray of objectsArray of files to update for the snippet. Required when the snippet has multiple files. Mutually exclusive with content, file_name. At least one of content, description, files, file_name, title, visibility must be provided
files[].action
Required
StringThe type of action to perform on the file, must be one of: create, update, delete, move
Allowed values: create, update, delete, move
Minimum length: 1
files[].contentStringContent of the snippet file
files[].file_pathStringPath of the snippet file
files[].previous_pathStringPrevious path of the snippet file
titleStringTitle of the snippet. At least one of content, description, files, file_name, title, visibility must be provided
visibilityStringVisibility level for the snippet. On GitLab.com, the internal value is not available. At least one of content, description, files, file_name, title, visibility must be provided
Allowed values: private, internal, public

Responses

CodeDescriptionSchema
200OKAPIEntitiesPersonalSnippet
400Validation error—
404Not found—
422Unprocessable entity—

Delete snippet

DELETE /api/v4/snippets/{id}

Deletes a specified snippet.

Parameters

NameTypeDescription
id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesPersonalSnippet
400Validation error—
404Not found—

Retrieve snippet file content

GET /api/v4/snippets/{id}/files/{ref}/{file_path}/raw

Retrieves the raw file content from a snippet as plain text.

Parameters

NameTypeDescription
ref
Path, required
StringThe name of branch, tag or commit
file_path
Path, required
StringThe URL-encoded path to the file, like lib%2Fclass%2Erb
id
Path, required
StringID of the snippet

Responses

CodeDescriptionSchema
200OK—
400Bad Request—
404Not found—

Retrieve a raw snippet

GET /api/v4/snippets/{id}/raw

Retrieves the raw contents of a specified snippet as plain text

Parameters

NameTypeDescription
id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OK—
400Bad Request—
404Not found—

Retrieve user agent details for a snippet

GET /api/v4/snippets/{id}/user_agent_detail

Retrieves user agent details for a specified snippet.

Parameters

NameTypeDescription
id
Path, required
IntegerID of the snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesUserAgentDetail
400Bad Request—
404Not found—

Schemas

Objects returned by the operations above and objects nested in their request bodies.

APIEntitiesCustomAttribute

PropertyTypeDescription
keyStringExample: foo
valueStringExample: bar

APIEntitiesPersonalSnippet

PropertyTypeDescription
authorAPIEntitiesUserBasic—
created_atString (date-time)Example: 2012-06-28T10:52:04Z
descriptionStringExample: Ruby test snippet
file_nameStringExample: add.rb
filesArray of objectsExample: [{"path":"file.txt","raw_url":"https://gitlab.example.com/.../raw"}]
http_url_to_repoStringExample: https://gitlab.example.com/snippets/65.git
idInteger (int64)Example: 1
importedBooleanExample: false
imported_fromStringExample: none
project_idInteger (int64)Example: 1
raw_urlStringExample: http://example.com/example/example/snippets/1/raw
ssh_url_to_repoStringExample: ssh://user@gitlab.example.com/snippets/65.git
titleStringExample: test
updated_atString (date-time)Example: 2012-06-28T10:52:04Z
visibilityStringExample: public
web_urlStringExample: http://example.com/example/example/snippets/1

APIEntitiesProjectSnippet

PropertyTypeDescription
authorAPIEntitiesUserBasic—
created_atString (date-time)Example: 2012-06-28T10:52:04Z
descriptionStringExample: Ruby test snippet
file_nameStringExample: add.rb
filesArray of objectsExample: [{"path":"file.txt","raw_url":"https://gitlab.example.com/.../raw"}]
http_url_to_repoStringExample: https://gitlab.example.com/snippets/65.git
idInteger (int64)Example: 1
importedBooleanExample: false
imported_fromStringExample: none
project_idInteger (int64)Example: 1
raw_urlStringExample: http://example.com/example/example/snippets/1/raw
ssh_url_to_repoStringExample: ssh://user@gitlab.example.com/snippets/65.git
titleStringExample: test
updated_atString (date-time)Example: 2012-06-28T10:52:04Z
visibilityStringExample: public
web_urlStringExample: http://example.com/example/example/snippets/1

APIEntitiesSnippet

PropertyTypeDescription
authorAPIEntitiesUserBasic—
created_atString (date-time)Example: 2012-06-28T10:52:04Z
descriptionStringExample: Ruby test snippet
file_nameStringExample: add.rb
filesArray of objectsExample: [{"path":"file.txt","raw_url":"https://gitlab.example.com/.../raw"}]
http_url_to_repoStringExample: https://gitlab.example.com/snippets/65.git
idInteger (int64)Example: 1
importedBooleanExample: false
imported_fromStringExample: none
project_idInteger (int64)Example: 1
raw_urlStringExample: http://example.com/example/example/snippets/1/raw
ssh_url_to_repoStringExample: ssh://user@gitlab.example.com/snippets/65.git
titleStringExample: test
updated_atString (date-time)Example: 2012-06-28T10:52:04Z
visibilityStringExample: public
web_urlStringExample: http://example.com/example/example/snippets/1

APIEntitiesUserAgentDetail

PropertyTypeDescription
akismet_submittedBooleanExample: false
ip_addressStringExample: 127.0.0.1
user_agentStringExample: AppleWebKit/537.36

APIEntitiesUserBasic

PropertyTypeDescription
avatar_pathStringExample: /user/avatar/28/The-Big-Lebowski-400-400.png
avatar_urlStringExample: https://gravatar.com/avatar/1
custom_attributesArray of APIEntitiesCustomAttribute—
idInteger (int64)Example: 1
lockedBoolean—
nameStringExample: Administrator
public_emailStringExample: john@example.com
stateStringExample: active
usernameStringExample: admin
web_urlStringExample: https://gitlab.example.com/root