Project access tokens API

You can read more about project access tokens.

List project access tokens

Introduced in GitLab 13.9.

Get a list of project access tokens.

GET projects/:id/access_tokens
AttributeTyperequiredDescription
idinteger or stringyesID or URL-encoded path of the project
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens"
[
   {
      "user_id" : 141,
      "scopes" : [
         "api"
      ],
      "name" : "token",
      "expires_at" : "2021-01-31",
      "id" : 42,
      "active" : true,
      "created_at" : "2021-01-20T22:11:48.151Z",
      "revoked" : false,
      "access_level": 40
   }
]

Get a project access token

Introduced in GitLab 14.10.

Get a project access token by ID.

GET projects/:id/access_tokens/:token_id
AttributeTyperequiredDescription
idinteger or stringyesID or URL-encoded path of the project
token_idinteger or stringyesID of the project access token
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>"
{
   "user_id" : 141,
   "scopes" : [
      "api"
   ],
   "name" : "token",
   "expires_at" : "2021-01-31",
   "id" : 42,
   "active" : true,
   "created_at" : "2021-01-20T22:11:48.151Z",
   "revoked" : false,
   "access_level": 40,
   "last_used_at": "2022-03-15T11:05:42.437Z"
}

Create a project access token

Version history

Create a project access token.

When you create a project access token, the maximum role (access level) you set depends on if you have the Owner or Maintainer role for the group. For example, the maximum role that can be set is:

  • Owner (50), if you have the Owner role for the project.
  • Maintainer (40), if you have the Maintainer role on the project.

In GitLab 14.8 and earlier, project access tokens have a maximum role of Maintainer.

POST projects/:id/access_tokens
AttributeTyperequiredDescription
idinteger or stringyesID or URL-encoded path of the project
nameStringyesName of the project access token
scopesArray[String]yesList of scopes
access_levelIntegernoAccess level. Valid values are 10 (Guest), 20 (Reporter), 30 (Developer), 40 (Maintainer), and 50 (Owner). Defaults to 40.
expires_atDateyesExpiration date of the access token in ISO format (YYYY-MM-DD). The date cannot be set later than the maximum allowable lifetime of an access token.
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type:application/json" \
--data '{ "name":"test_token", "scopes":["api", "read_repository"], "expires_at":"2021-01-31", "access_level": 30 }' \
"https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens"
{
   "scopes" : [
      "api",
      "read_repository"
   ],
   "active" : true,
   "name" : "test",
   "revoked" : false,
   "created_at" : "2021-01-21T19:35:37.921Z",
   "user_id" : 166,
   "id" : 58,
   "expires_at" : "2021-01-31",
   "token" : "D4y...Wzr",
   "access_level": 30
}

Rotate a project access token

Introduced in GitLab 16.0

Rotate a project access token. Revokes the previous token and creates a new token that expires in one week.

POST /projects/:id/access_tokens/:token_id/rotate
AttributeTyperequiredDescription
idinteger or stringyesID or URL-encoded path of the project
token_idinteger or stringyesID of the project access token
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>/rotate"

Example response:

{
    "id": 42,
    "name": "Rotated Token",
    "revoked": false,
    "created_at": "2023-08-01T15:00:00.000Z",
    "scopes": ["api"],
    "user_id": 1337,
    "last_used_at": null,
    "active": true,
    "expires_at": "2023-08-15",
    "access_level": 30,
    "token": "s3cr3t"
}

Responses

  • 200: OK if the existing token is successfully revoked and the new token is successfully created.
  • 400: Bad Request if not rotated successfully.
  • 401: Unauthorized if either the:
    • User does not have access to the token with the specified ID.
    • Token with the specified ID does not exist.
  • 404: Not Found if the user is an administrator but the token with the specified ID does not exist.

Automatic reuse detection

Refer to automatic reuse detection for personal access tokens for more information.

Revoke a project access token

Introduced in GitLab 13.9.

Revoke a project access token.

DELETE projects/:id/access_tokens/:token_id
AttributeTyperequiredDescription
idinteger or stringyesID or URL-encoded path of the project
token_idinteger or stringyesID of the project access token
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>"

Responses

  • 204: No Content if successfully revoked.
  • 400 Bad Request or 404 Not Found if not revoked successfully.