> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gitar.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Rotate the GitLab access token

> Replace the GitLab access token Gitar uses with a newer token for the same GitLab user, e.g. right after GitLab issues it. Gitar validates the new token against the connected instance and group before storing it, and the previous token stays in use if validation fails.

Requires an API token with the `gitlab_token:rotate` scope, which Administrator tokens include. The new token must authenticate as the same GitLab user as the current one. To connect a different user, update the token in Gitar's GitLab settings.

**Selecting a GitLab instance.** Organizations with a single connected GitLab instance can omit `host`. Once an organization connects more than one instance, `host` is required.

Replace the GitLab access token Gitar uses, e.g. from the job that rotates it in GitLab. Gitar validates the new token against your connected instance and group before storing it. If validation fails, the current token stays in use.

## Prerequisites

| Requirement | Setup |
| - | - |
| Connected GitLab instance | [Connect GitLab](https://docs.gitar.ai/connecting-code/gitlab.md) to Gitar with a personal or service account token. |
| API token with `gitlab_token:rotate` | In **Settings → API Tokens**, create a token with key type **GitLab token rotation**, which can only rotate the GitLab token. **Administrator** tokens also work. |

## Restrictions

The new token must authenticate as the same GitLab user as the current token. This endpoint renews a credential and does not switch accounts. To connect a different GitLab user, update the token from the GitLab settings page in Gitar.

The new token must pass the same checks as the connect form, including the `api` scope and access to the connected group.

Each rotation is recorded in the organization's audit log with the API token that made it.

## Example

```bash theme={null}
curl -X POST https://api.gitar.ai/v1/external/gitlab/token \
  -H "Authorization: Bearer $GITAR_ROTATION_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"access_token\": \"$NEW_GITLAB_TOKEN\"}"
```

Organizations with more than one connected GitLab instance must also pass `host`, e.g. `"host": "https://gitlab.example.com"`.


## OpenAPI

````yaml post /external/gitlab/token
openapi: 3.1.0
info:
  title: Gitar External API
  description: Read review data and manage your Gitar installation and integrations
  contact:
    name: Gitar
    url: https://gitar.ai
    email: noreply@gitar.ai
  license:
    name: ''
  version: 1.0.0
servers:
  - url: https://api.gitar.ai/v1
security: []
paths:
  /external/gitlab/token:
    post:
      tags:
        - GitLab Token
      summary: Rotate the GitLab access token
      description: >-
        Replace the GitLab access token Gitar uses with a newer token for the
        same GitLab user, e.g. right after GitLab issues it. Gitar validates the
        new token against the connected instance and group before storing it,
        and the previous token stays in use if validation fails.


        Requires an API token with the `gitlab_token:rotate` scope, which
        Administrator tokens include. The new token must authenticate as the
        same GitLab user as the current one. To connect a different user, update
        the token in Gitar's GitLab settings.


        **Selecting a GitLab instance.** Organizations with a single connected
        GitLab instance can omit `host`. Once an organization connects more than
        one instance, `host` is required.
      operationId: rotateGitlabToken
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RotateGitlabTokenApiRequest'
        required: true
      responses:
        '200':
          description: Token validated and stored
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RotateGitlabTokenApiResponse'
        '400':
          description: The new token failed validation, or no GitLab instance is connected
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseFailure'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseFailure'
        '403':
          description: >-
            The API token lacks the `gitlab_token:rotate` scope, or the new
            token belongs to a different GitLab user
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseFailure'
        '409':
          description: >-
            `host` did not identify one connected instance, or the connected
            GitLab user is not recorded yet
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseFailure'
      security:
        - bearerAuth: []
components:
  schemas:
    RotateGitlabTokenApiRequest:
      type: object
      description: Request to replace the connected GitLab token.
      required:
        - access_token
      properties:
        access_token:
          type: string
          description: >-
            The new GitLab personal or service account access token. It must
            authenticate as

            the same GitLab user as the token it replaces.
        host:
          type:
            - string
            - 'null'
          description: >-
            GitLab instance to update, e.g. `https://gitlab.example.com`.
            Required once the

            organization connects more than one instance.
    RotateGitlabTokenApiResponse:
      type: object
      description: Result of a successful token rotation.
      required:
        - host
      properties:
        expires_at_ms:
          type:
            - integer
            - 'null'
          format: int64
          description: >-
            When GitLab says the new token expires, in milliseconds since the
            Unix epoch.

            `null` when the token has no expiry.
          minimum: 0
        host:
          type: string
          description: The GitLab instance whose token was replaced.
    ResponseFailure:
      type: object
      required:
        - message
      properties:
        error_code:
          type:
            - string
            - 'null'
        message:
          type: string
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.