Authentication overview
The GitLab API provides programmatic access to a wide range of GitLab features, enabling automation, integration with external systems, and custom application development. Securely interacting with the API requires proper authentication to verify the identity and authorization of the requesting client. GitLab supports several authentication methods, each designed for specific use cases and offering different levels of security and control. The choice of authentication method depends on the application's nature, whether it's a personal script, a server-side application, or a client-side web application.
All interactions with the GitLab API must occur over HTTPS to ensure data encryption in transit. Authentication credentials, such as tokens, are sent with each API request, typically in the Private-Token header for Personal Access Tokens or the Authorization header for OAuth 2.0 tokens.
Supported authentication methods
GitLab API supports three primary methods for authentication:
- Personal Access Tokens (PATs): These are secret tokens that grant access to the GitLab API on behalf of a user. They are suitable for personal scripts, command-line tools, and integrations where a dedicated service account is not feasible. PATs can be scoped to specific permissions (e.g.,
read_api,write_repository) and can be set with an expiration date, as detailed in the GitLab Personal Access Token documentation. - OAuth 2.0: This open standard is designed for delegated authorization, allowing third-party applications to access user resources without exposing user credentials. OAuth 2.0 is recommended for web applications, mobile applications, and other scenarios where users grant access to an application. GitLab supports several OAuth 2.0 grant types, including the Authorization Code flow for web applications and the Client Credentials flow for server-to-server integrations. More information can be found in the GitLab OAuth 2.0 provider guide. The general principles of OAuth 2.0 are described by the OAuth 2.0 framework.
- CI/CD Job Tokens: These tokens are automatically generated for each CI/CD job and provide temporary, scoped access to the GitLab API within the context of a running pipeline. They are ideal for automating tasks within GitLab CI/CD, such as updating project labels, triggering other pipelines, or interacting with GitLab's container registry. Job tokens inherit the permissions of the user who initiated the pipeline and are valid only for the duration of the job, as explained in the GitLab CI/CD Job Token documentation.
Authentication method comparison
| Method | When to Use | Security Level / Best Practice |
|---|---|---|
| Personal Access Tokens (PATs) | Personal scripts, command-line tools, integrations not requiring user consent flows. | Moderate. Scope permissions, set expiration dates, treat as passwords. |
| OAuth 2.0 | Third-party applications, web/mobile apps, service-to-service integrations (e.g., Authorization Code, Client Credentials). | High. Delegated authorization, refresh tokens, explicit user consent. Recommended for public applications. |
| CI/CD Job Tokens | Automated tasks within GitLab CI/CD pipelines. | High. Short-lived, automatically generated, scoped to job context. |
Getting your credentials
Personal Access Tokens (PATs)
To generate a Personal Access Token:
- Log in to your GitLab instance.
- Navigate to your user settings by clicking your avatar in the top right corner and selecting Preferences.
- In the left sidebar, select Access Tokens.
- Enter a Token name and an optional Expiration date.
- Select the desired Scopes. For most API interactions,
apiscope is broad, while more specific scopes likeread_repositoryorwrite_repositoryoffer granular control. - Click Create personal access token.
- Copy the generated token immediately. It will not be shown again.
OAuth 2.0 Application credentials
To set up an OAuth 2.0 application:
- Log in to your GitLab instance.
- Navigate to your user settings by clicking your avatar in the top right corner and selecting Preferences.
- In the left sidebar, select Applications.
- Click New application.
- Provide an Application name.
- Enter one or more Redirect URI(s). These are the URLs where GitLab will redirect the user after they authorize your application.
- Select the desired Scopes for your application.
- (Optional) Provide an Application URL and Confidential checkbox status.
- Click Save application.
- GitLab will provide a Application ID (Client ID) and a Secret (Client Secret). Store these securely.
CI/CD Job Tokens
CI/CD Job Tokens are automatically available within GitLab CI/CD jobs via the predefined environment variable CI_JOB_TOKEN. No manual generation is required. For example, within a .gitlab-ci.yml file, you can directly use this variable in your scripts.
Authenticated request example
Using a Personal Access Token (PAT)
This example demonstrates fetching a user's projects using a PAT with cURL:
curl --header "Private-Token: <your_personal_access_token>" \
"https://gitlab.com/api/v4/projects"
Replace <your_personal_access_token> with your actual PAT and adjust the URL for your GitLab instance if it's self-hosted.
Using an OAuth 2.0 Access Token
After completing an OAuth 2.0 flow (e.g., Authorization Code flow) and obtaining an access token, you would use it in the Authorization header:
curl --header "Authorization: Bearer <your_oauth_access_token>" \
"https://gitlab.com/api/v4/user"
Replace <your_oauth_access_token> with the token obtained from the OAuth 2.0 flow.
Using a CI/CD Job Token
Within a GitLab CI/CD job, the CI_JOB_TOKEN environment variable provides the necessary authentication:
# .gitlab-ci.yml
my-script-job:
stage: build
script:
- curl --header "JOB-TOKEN: $CI_JOB_TOKEN" \
"https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/pipelines"
This example fetches pipelines for the current project using the automatically supplied job token.
Security best practices
- Use HTTPS: Always interact with the GitLab API over HTTPS to encrypt communications and prevent eavesdropping. All GitLab API endpoints enforce HTTPS.
- Least Privilege: Grant only the necessary scopes or permissions to your tokens and applications. For example, if an application only needs to read repository data, do not grant it write permissions. This principle is fundamental in securing API access, as emphasized in general API security guidelines by providers like Stripe's API security recommendations.
- Set Expiration Dates: For Personal Access Tokens, always set an expiration date. This limits the window of vulnerability if a token is compromised. Regularly rotate tokens.
- Store Credentials Securely: Never hardcode tokens or application secrets directly into your source code. Use environment variables, secret management services, or secure configuration files.
- Revoke Compromised Tokens: If you suspect a token has been compromised, revoke it immediately through the GitLab UI or API.
- Monitor API Usage: Regularly review API logs and audit events to detect unusual activity or potential misuse of tokens.
- Use OAuth 2.0 for Third-Party Applications: For applications interacting on behalf of users, OAuth 2.0 is the most secure method as it avoids sharing user credentials directly with the application and provides a robust framework for delegated authorization.
- Limit IP Access (Self-Hosted): For self-hosted GitLab instances, consider configuring network access controls to limit API access to known IP addresses or ranges.