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 integerThe ID or URL-encoded path of the project
page
Query
IntegerCurrent page number
Default: 1
Example: 1
per_page
Query
IntegerNumber of items per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKAPIEntitiesProjectSnippet
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 integerThe ID or URL-encoded path of the project

Request body (application/json)

PropertyTypeDescription
contentStringThe content of a snippet. Mutually exclusive with files
descriptionStringThe description of a snippet
file_name
Required
StringThe name of a snippet file
filesArray of objectsAn array of files. Mutually exclusive with content
files[].content
Required
StringThe content of a snippet file
files[].file_path
Required
StringThe path of a snippet file
title
Required
StringThe title of the snippet
visibility
Required
StringThe visibility of the snippet
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 integerThe ID or URL-encoded path of the project
snippet_id
Path, required
IntegerThe ID of a project 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 integerThe ID or URL-encoded path of the project
snippet_id
Path, required
IntegerThe ID of a project snippet

Request body (application/json)

PropertyTypeDescription
contentStringThe content of the snippet. Mutually exclusive with files
descriptionStringThe description of a snippet
file_nameStringThe file name of the snippet. Mutually exclusive with files
filesArray of objectsAn array of files to update. Mutually exclusive with content, file_name
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[].contentStringThe content of a snippet
files[].file_pathStringThe file path of a snippet file
files[].previous_pathStringThe previous path of a snippet file
titleStringThe title of the snippet
visibilityStringThe visibility of the snippet
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 integerThe ID or URL-encoded path of the project
snippet_id
Path, required
IntegerThe ID of a project 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 integerThe ID 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
IntegerThe ID of a project 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 integerThe ID or URL-encoded path of the project
snippet_id
Path, required
IntegerThe ID of a project 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 integerThe ID or URL-encoded path of the project
snippet_id
Path, required
IntegerThe ID of a project 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 after the specified time
created_before
Query
String (date-time)Return snippets created before the specified time
page
Query
IntegerCurrent page number
Default: 1
Example: 1
per_page
Query
IntegerNumber of items per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKAPIEntitiesSnippet
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
descriptionStringThe description of a snippet
file_name
Required
StringThe name of a snippet file
filesArray of objectsAn array of files. Mutually exclusive with content
files[].content
Required
StringThe content of a snippet file
files[].file_path
Required
StringThe path of a snippet file
title
Required
StringThe title of a snippet
visibilityStringThe visibility of the snippet
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 after the specified time
created_before
Query
String (date-time)Return snippets created before the specified time
page
Query
IntegerCurrent page number
Default: 1
Example: 1
per_page
Query
IntegerNumber of items per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKAPIEntitiesSnippet
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 after the specified time
created_before
Query
String (date-time)Return snippets created before the specified time
page
Query
IntegerCurrent page number
Default: 1
Example: 1
per_page
Query
IntegerNumber of items per page
Default: 20
Example: 20

Responses

CodeDescriptionSchema
200OKAPIEntitiesPersonalSnippet
400Bad Request—
404Not found—

Retrieve a snippet

GET /api/v4/snippets/{id}

Retrieves a specified snippet.

Parameters

NameTypeDescription
id
Path, required
IntegerThe ID of a snippet

Responses

CodeDescriptionSchema
200OKAPIEntitiesPersonalSnippet
400Bad Request—
404Not found—

Update snippet

PUT /api/v4/snippets/{id}

Updates a specified snippet.

Parameters

NameTypeDescription
id
Path, required
IntegerThe ID of a snippet

Request body (application/json)

PropertyTypeDescription
contentStringThe content of a snippet. Mutually exclusive with files
descriptionStringThe description of a snippet
file_nameStringThe name of a snippet file. Mutually exclusive with files
filesArray of objectsAn array of files to update. Mutually exclusive with content, file_name
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[].contentStringThe content of a snippet
files[].file_pathStringThe file path of a snippet file
files[].previous_pathStringThe previous path of a snippet file
titleStringThe title of a snippet
visibilityStringThe visibility of the snippet
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
IntegerThe ID of a 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
StringThe ID of a 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
IntegerThe ID of a 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
IntegerThe ID of a 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