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.
note
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

Parameters:

AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.

Single snippet

Get a single project snippet.

GET /projects/:id/snippets/:snippet_id

Parameters:

AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
snippet_idintegeryesThe 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",
  "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:

AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
files:contentstringyesContent of the snippet file.
files:file_pathstringyesFile path of the snippet file.
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.
filesarray of hashesnoAn array of snippet files.
visibilitystringnoSnippet’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:

AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
files:actionstringyesType of action to perform on the file. One of: create, update, delete, move.
snippet_idintegeryesThe ID of a project’s snippet.
contentstringnoDeprecated: Use files instead. Content of a snippet.
descriptionstringnoDescription of a snippet.
filesarray of hashesnoAn array of snippet files.
files:contentstringnoContent of the snippet file.
files:file_pathstringnoFile path of the snippet file.
file_namestringnoDeprecated: Use files instead. Name of a snippet file.
files:previous_pathstringnoPrevious path of the snippet file.
titlestringnoTitle of a snippet.
visibilitystringnoSnippet’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:

AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
snippet_idintegeryesThe 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:

AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
snippet_idintegeryesThe 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:

AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
file_pathstringyesThe URL-encoded path to the file, for example, snippet%2Erb.
refstringyesThe name of a branch, tag or commit, for example, main.
snippet_idintegeryesThe 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
AttributeTypeRequiredDescription
idinteger or stringyesThe ID or URL-encoded path of the project owned by the authenticated user.
snippet_idIntegeryesThe ID of a snippet.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" "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
}