Developer tools

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

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.

GitLab key types

TypePrefixSecret?ScopeExpiry
Personal access token
The prefix is configurable by administrators. Expired tokens cannot be reactivated.
glpat-YesSelected scopes (such as `api`); inherits the creating user's permissionsExpires at midnight UTC on the chosen date; default and standard maximum 365 days
Project access tokenglpat-YesOne project, backed by a bot user—
Group access tokenglpat-YesOne group, backed by a bot user—
Deploy tokengldt-YesClone, push and pull packages and registry images; no API access—
CI/CD job tokenglcbt-YesA limited set of API endpointsValid 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.

Rotating makes the original token inactive immediately and cannot be undone, so tools fail until updated. Users get email notices 60, 30 and 7 days before expiry.

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 your GitLab 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.

Things that catch people out

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

Common GitLab authentication errors

insufficient_scope

A legacy personal, project or group access token was sent to the GitLab MCP server, which rejects them even with the api scope (403 Forbidden).

Recent incidents that put GitLab keys at risk

If you were affected by one of these, rotate your GitLab keys using the rotation steps above.

Using your GitLab key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a GitLab key, these intents fit:

IntentWhat BotPassword does
list_credentialsLists the credentials this agent is granted and what it may do with each. Never values.
request_accessAsks 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_withRuns 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_requestMakes 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_loginLets 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.
rotatePartly 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_secretPlanned, not built yet: will read a newly created key straight from the page or command output into the vault.
store_secretSaves a value the agent already holds as a new vault item.

GitLab also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

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.

Stop pasting GitLab keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.