# GitLab API tokens: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/gitlab
Last checked: 2026-10-25

GitLab personal access tokens are created under your avatar → Edit profile → Access → Personal access tokens and default to a 365-day lifetime. Tokens start with `glpat-` by default; project and group access tokens share that prefix. GitLab recommends job tokens, then project or group tokens, over personal tokens in CI/CD.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Personal access token | `glpat-` | Yes | Selected scopes (such as `api`); inherits the creating user's permissions | Expires at midnight UTC on the chosen date; default and standard maximum 365 days |
| Project access token | `glpat-` | Yes | One project, backed by a bot user | — |
| Group access token | `glpat-` | Yes | One group, backed by a bot user | — |
| Deploy token | `gldt-` | Yes | Clone, push and pull packages and registry images; no API access | — |
| CI/CD job token | `glcbt-` | Yes | A limited set of API endpoints | Valid only for the duration of the job |

## How to create a GitLab key

1. Select your avatar, then **Edit profile**.
2. Open **Access** → **Personal access tokens**.
3. From the **Generate token** dropdown choose **Legacy token** (or a fine-grained option if offered).
4. Enter a name, optional description, an expiry date and at least one scope.
5. Generate the token and copy it; it is shown once.

## Limiting what the key can do

Choose the narrowest scopes possible and use separate tokens for read and write. Tokens inherit the creating user's permissions, so project or group access tokens (bot users) limit blast radius better than personal tokens. The GitLab MCP server rejects legacy tokens and requires either an OAuth token with the `mcp` scope or a fine-grained personal access token with the MCP tool Execute permission.

## How to rotate a GitLab key

1. Open the token in **Personal access tokens**, open the ellipsis menu and select **Rotate**.
2. Copy the new token and update every tool that used the old one.

## How to revoke a GitLab key

1. Open **Personal access tokens**, open the ellipsis menu on the token and select **Revoke**.
2. Revocation is immediate and cannot be undone; GitLab keeps the revoked token record for audit.

## If the key leaked

1. Revoke the token immediately from the ellipsis menu.
2. Create a replacement with narrower scopes and a short expiry.
3. Review recent activity for the user, project or group the token could reach.
4. Replace any token that was pasted into issues, merge requests, comments or logs, and remove it from those places.

## Gotchas

- The token is shown once at creation; there is no way to view it again.
- Send tokens in the `PRIVATE-TOKEN` header, not in URLs, because URLs can be logged.
- Revoked tokens cannot be permanently deleted, only revoked.
- In CI/CD avoid personal tokens; GitLab prefers job tokens, then project tokens, then group tokens.

## Using this key with AI agents (BotPassword)

- `list_credentials`: Lists the credentials this agent is granted and what it may do with each. Never values.
- `request_access`: Asks you for a grant, which you approve with your master password or decline in BotPassword on your desktop, never in the chat. The agent gets a receipt, never a value. Approval on your phone is planned.
- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `http_request`: Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.
- `cli_login`: Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned.
- `rotate`: Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
- `capture_secret`: Planned, not built yet: will read a newly created key straight from the page or command output into the vault.
- `store_secret`: Saves a value the agent already holds as a new vault item.

## FAQ

### Where do I create a GitLab personal access token?

Select your avatar, then **Edit profile** → **Access** → **Personal access tokens**, and generate a token with a name, expiry and scopes.

### How long does a GitLab personal access token last?

It expires at midnight UTC on the date you choose, defaulting to 365 days. Administrators can change the maximum.

### How do I rotate a GitLab personal access token?

Use the ellipsis menu on the token and select **Rotate**. The new token keeps the same scopes and the old one stops working immediately.

### What is the difference between a personal and a project access token on GitLab?

A personal token acts as you with your permissions. A project access token is scoped to one project and backed by a bot user.

### Can an AI agent use my GitLab key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `list_credentials`, `run_with`, `http_request`, so the agent gets the result and never the value.

## Sources

- https://docs.gitlab.com/user/profile/personal_access_tokens/
- https://docs.gitlab.com/security/tokens/
- https://docs.gitlab.com/user/model_context_protocol/mcp_server/
