Project snippets

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

Use the project snippet API to create, manage, and delete 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.

From July 2019, the 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

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.

If successful, returns 200 OK and the following response attributes:

AttributeTypeDescription
author.created_atstringDate and time when the author account was created.
author.emailstringEmail address of the snippet author.
author.idintegerID of the snippet author.
author.namestringDisplay name of the snippet author.
author.statestringState of the author account.
author.usernamestringUsername of the snippet author.
created_atstringDate and time when the snippet was created in ISO 8601 format.
descriptionstringDescription of the snippet.
file_namestringName of the snippet file.
idintegerID of the snippet.
importedbooleanIf true, the snippet was imported.
imported_fromstringSource of the import if the snippet was imported.
project_idintegerID of the project containing the snippet.
raw_urlstringDirect URL to the raw snippet content.
titlestringTitle of the snippet.
updated_atstringDate and time when the snippet was last updated in ISO 8601 format.
web_urlstringURL to view the snippet in the GitLab web interface.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/snippets"

Example response:

[
  {
    "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"
  },
  {
    "id": 3,
    "title": "Configuration helper",
    "file_name": "config.yml",
    "description": "YAML configuration snippet",
    "author": {
      "id": 2,
      "username": "jane_doe",
      "email": "jane@example.com",
      "name": "Jane Doe",
      "state": "active",
      "created_at": "2013-02-15T10:30:20Z"
    },
    "updated_at": "2013-03-10T14:15:30Z",
    "created_at": "2013-03-01T09:45:12Z",
    "imported": false,
    "imported_from": "none",
    "project_id": 1,
    "web_url": "http://example.com/example/example/snippets/3",
    "raw_url": "http://example.com/example/example/snippets/3/raw"
  }
]

Get single snippet

Get a single project snippet.

GET /projects/:id/snippets/:snippet_id

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
snippet_idintegerYesID of a project’s snippet.

If successful, returns 200 OK and the following response attributes:

AttributeTypeDescription
author.created_atstringDate and time when the author account was created.
author.emailstringEmail address of the snippet author.
author.idintegerID of the snippet author.
author.namestringDisplay name of the snippet author.
author.statestringState of the author account.
author.usernamestringUsername of the snippet author.
created_atstringDate and time when the snippet was created in ISO 8601 format.
descriptionstringDescription of the snippet.
file_namestringName of the snippet file.
idintegerID of the snippet.
importedbooleanIf true, the snippet was imported.
imported_fromstringSource of the import if the snippet was imported.
project_idintegerID of the project containing the snippet.
raw_urlstringDirect URL to the raw snippet content.
titlestringTitle of the snippet.
updated_atstringDate and time when the snippet was last updated in ISO 8601 format.
web_urlstringURL to view the snippet in the GitLab web interface.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/snippets/2"

Example response:

{
  "id": 2,
  "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/2",
  "raw_url": "http://example.com/example/example/snippets/2/raw"
}

Create new snippet

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

POST /projects/:id/snippets

Supported attributes:

AttributeTypeRequiredDescription
filesarray of hashesYesAn array of snippet files.
files:contentstringYesContent of the snippet file.
files:file_pathstringYesFile path of the snippet file.
idinteger or stringYesID or URL-encoded path of the project.
titlestringYesTitle of a snippet.
contentstringNoDeprecated: Use files instead. Content of a snippet.
descriptionstringNoDescription of a snippet.
file_namestringNoDeprecated: Use files instead. Name of a snippet file.
visibilitystringNoSnippet’s visibility.

If successful, returns 201 Created and the following response attributes:

AttributeTypeDescription
author.created_atstringDate and time when the author account was created.
author.emailstringEmail address of the snippet author.
author.idintegerID of the snippet author.
author.namestringDisplay name of the snippet author.
author.statestringState of the author account.
author.usernamestringUsername of the snippet author.
created_atstringDate and time when the snippet was created in ISO 8601 format.
descriptionstringDescription of the snippet.
file_namestringName of the snippet file.
idintegerID of the snippet.
importedbooleanIf true, the snippet was imported.
imported_fromstringSource of the import if the snippet was imported.
project_idintegerID of the project containing the snippet.
raw_urlstringDirect URL to the raw snippet content.
titlestringTitle of the snippet.
updated_atstringDate and time when the snippet was last updated in ISO 8601 format.
web_urlstringURL to view the snippet in the GitLab web interface.

Example request:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --header "Content-Type: application/json" \
  --data '{"title": "Example Snippet Title", "description": "More verbose snippet description", "visibility": "private", "files": [{"file_path": "example.txt", "content": "source code \n with multiple lines\n"}]}' \
  --url "https://gitlab.example.com/api/v4/projects/1/snippets"

Example response:

{
  "id": 1,
  "title": "Example Snippet Title",
  "file_name": "example.txt",
  "description": "More verbose snippet description",
  "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"
}

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

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
snippet_idintegerYesID of a project’s snippet.
files:actionstringConditionallyType of action to perform on the file. One of: create, update, delete, move. Required when using the files attribute.
contentstringNoDeprecated: Use files instead. Content of a snippet.
descriptionstringNoDescription of a snippet.
file_namestringNoDeprecated: Use files instead. Name of a snippet file.
filesarray of hashesNoAn array of snippet files.
files:contentstringNoContent of the snippet file.
files:file_pathstringNoFile path of the snippet file.
files:previous_pathstringNoPrevious path of the snippet file.
titlestringNoTitle of a snippet.
visibilitystringNoSnippet’s visibility.

If successful, returns 200 OK and the following response attributes:

AttributeTypeDescription
author.created_atstringDate and time when the author account was created.
author.emailstringEmail address of the snippet author.
author.idintegerID of the snippet author.
author.namestringDisplay name of the snippet author.
author.statestringState of the author account.
author.usernamestringUsername of the snippet author.
created_atstringDate and time when the snippet was created in ISO 8601 format.
descriptionstringDescription of the snippet.
file_namestringName of the snippet file.
idintegerID of the snippet.
importedbooleanIf true, the snippet was imported.
imported_fromstringSource of the import if the snippet was imported.
project_idintegerID of the project containing the snippet.
raw_urlstringDirect URL to the raw snippet content.
titlestringTitle of the snippet.
updated_atstringDate and time when the snippet was last updated in ISO 8601 format.
web_urlstringURL to view the snippet in the GitLab web interface.

Example request:

curl --request PUT \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --header "Content-Type: application/json" \
  --data '{"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"}]}' \
  --url "https://gitlab.example.com/api/v4/projects/1/snippets/2"

Example response:

{
  "id": 2,
  "title": "Updated Snippet Title",
  "file_name": "example.txt",
  "description": "More verbose snippet description",
  "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/2",
  "raw_url": "http://example.com/example/example/snippets/2/raw"
}

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

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
snippet_idintegerYesID of a project’s snippet.

Example request:

curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/snippets/2"

Snippet content

Returns the raw project snippet as plain text.

GET /projects/:id/snippets/:snippet_id/raw

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
snippet_idintegerYesID of a project’s snippet.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/snippets/2/raw"

Snippet repository file content

Returns the raw file content as plain text.

GET /projects/:id/snippets/:snippet_id/files/:ref/:file_path/raw

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
file_pathstringYesURL-encoded path to the file, for example, snippet%2Erb.
refstringYesName of a branch, tag or commit, for example, main.
snippet_idintegerYesID of a project’s snippet.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw"

Get user agent details

Available only for users with administrator access.

GET /projects/:id/snippets/:snippet_id/user_agent_detail

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
snippet_idintegerYesID of a snippet.

If successful, returns 200 OK and the following response attributes:

AttributeTypeDescription
akismet_submittedbooleanIf true, the snippet was submitted to Akismet for spam detection.
ip_addressstringIP address of the user who created the snippet.
user_agentstringUser agent string of the browser used to create the 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
}