Authentication overview

The Linode API provides programmatic access to manage your Linode cloud infrastructure, including compute instances, storage, networking, and databases. To interact with the API, requests must be authenticated to verify the identity of the user or application making the request and to ensure they have the necessary permissions. The Linode API adheres to RESTful principles and uses standard HTTP authentication schemes.

Authentication is a critical security layer that prevents unauthorized access and modifications to your cloud resources. By correctly implementing the supported authentication methods, developers can integrate Linode services into their applications, scripts, and automation workflows while maintaining control over access privileges. The API supports two primary authentication mechanisms: Personal Access Tokens (PATs) for direct user and script access, and OAuth 2.0 for third-party applications.

Understanding the differences between these methods and their appropriate use cases is fundamental for secure and efficient interaction with the Linode API. Each method provides a way to generate a credential that is then included in the HTTP Authorization header of API requests, typically as a Bearer token, as defined in the OAuth 2.0 Bearer Token Usage specification.

Supported authentication methods

The Linode API supports two main authentication methods, each designed for specific use cases and security requirements.

Personal Access Tokens (PATs)

Personal Access Tokens (PATs) are the most common method for authenticating with the Linode API for individual users, scripts, and command-line tools. A PAT is a string of characters that acts as a substitute for your username and password, granting access to your Linode account resources based on the permissions (scopes) assigned to it. PATs are considered a form of token-based authentication.

  • Use Case: Ideal for personal scripts, automation, CLI usage, and applications that directly manage a single Linode account.
  • Security: PATs should be treated like passwords. They grant direct access and their compromise can lead to unauthorized control over your Linode resources. Granular scopes and expiration dates help mitigate risks.
  • Scopes: When generating a PAT, you define its permissions, known as scopes. These scopes determine which API endpoints and actions the token can access (e.g., read-only access to compute instances, full access to object storage).
  • Expiration: PATs can be configured with an expiration date, ranging from a few hours to one year, or set to never expire. It is a security best practice to set the shortest possible expiration period required for the token's purpose.

OAuth 2.0

OAuth 2.0 is an authorization framework that enables third-party applications to obtain limited access to a user's Linode account without exposing the user's credentials. It is an industry-standard protocol for delegated authorization, as detailed in the OAuth 2.0 specification.

  • Use Case: Primarily used by third-party applications or services that need to interact with a user's Linode account on their behalf. For example, a dashboard application that shows Linode resource usage across multiple user accounts.
  • Security: OAuth 2.0 enhances security by allowing users to grant specific permissions to applications without sharing their login credentials. The application receives an access token, which is typically short-lived and tied to specific scopes.
  • Flow: The typical OAuth 2.0 flow involves the user being redirected to Linode's authentication page, where they log in and grant permission to the third-party application. Linode then redirects the user back to the application with an authorization code, which the application exchanges for an access token and optionally a refresh token.
  • Client Credentials: Third-party applications must be registered with Linode to obtain a Client ID and Client Secret, which are used during the OAuth flow.

Here's a comparison of the two methods:

Method When to Use Security Level & Considerations
Personal Access Token (PAT) Individual users, scripts, CLI, direct automation. High: When managed carefully. Treat as a password. Granular scopes and expiration dates are crucial. Direct access to your account.
OAuth 2.0 Third-party applications, services acting on behalf of users. High: For delegated authorization. Users grant specific permissions without sharing credentials. More complex setup for developers, but more secure for end-users.

Getting your credentials

The process for obtaining credentials depends on the authentication method you choose.

Generating a Personal Access Token

To generate a PAT, you typically use the Linode Cloud Manager or the Linode CLI.

  1. Via Linode Cloud Manager:
    1. Log in to the Linode Cloud Manager.
    2. Navigate to the API Tokens section under your profile.
    3. Click Add a Personal Access Token.
    4. Provide a descriptive label for the token (e.g., "My-Script-Token").
    5. Set an expiration date. For enhanced security, choose the shortest necessary duration.
    6. Configure the desired Access Scopes. Carefully select only the permissions required for the token's intended purpose. For example, if your script only needs to read instance information, grant linodes:read_only.
    7. Click Create Token.
    8. The token will be displayed once. Copy it immediately, as it will not be shown again. Store it securely.
  2. Via Linode CLI:
    1. Ensure you have the Linode CLI installed and configured. If not, follow the Linode CLI installation guide.
    2. Run the command: linode-cli tokens create --label "My-CLI-Token" --expiry "2027-05-29T00:00:00Z" --scopes "linodes:read_write" (adjust label, expiry, and scopes as needed).
    3. The CLI will output the generated token. Copy and store it securely.

Setting up OAuth 2.0 Applications

For OAuth 2.0, you need to register your application with Linode to obtain a Client ID and Client Secret.

  1. Register Your Application:
      ">
    1. Log in to the Linode Cloud Manager.
    2. Navigate to the OAuth Clients section under your profile.
    3. Click Add an OAuth Client.
    4. Provide a label, a description, and a callback URL (the URL where Linode will redirect users after authorization).
    5. Click Create Client.
    6. Linode will provide you with a Client ID and a Client Secret. Store these securely; the Client Secret should be treated with the same care as a password.
  2. Implement the OAuth Flow: Your application will then need to implement the standard OAuth 2.0 authorization code grant flow. This involves redirecting users to Linode's authorization endpoint, handling the callback, and exchanging the authorization code for an access token using your Client ID and Client Secret. Detailed instructions are available in the Linode API OAuth documentation.

Authenticated request example

Once you have obtained a Personal Access Token (PAT), you can include it in your API requests using the Authorization HTTP header with the Bearer scheme. This is a common pattern for HTTP authentication.

cURL example

This example demonstrates how to retrieve a list of your Linode instances using a PAT with cURL:

curl -H "Authorization: Bearer <YOUR_LINODE_PAT>" \
     -H "Content-Type: application/json" \
     https://api.linode.com/v4/linode/instances

Replace <YOUR_LINODE_PAT> with your actual Personal Access Token.

Python SDK example

If you are using one of the official Linode SDKs, the authentication process is often abstracted. Here's an example using the Linode Python SDK:

import os
import linode_api4

# It's recommended to store your token in an environment variable
# For example: export LINODE_CLI_TOKEN="your_pat_here"
TOKEN = os.environ.get("LINODE_CLI_TOKEN")

if not TOKEN:
    print("Error: LINODE_CLI_TOKEN environment variable not set.")
    exit(1)

client = linode_api4.LinodeClient(TOKEN)

try:
    # Fetch all Linode instances
    instances = client.linode.instances()

    if instances:
        print("Your Linode Instances:")
        for instance in instances:
            print(f"  - ID: {instance.id}, Label: {instance.label}, Type: {instance.type.id}, Status: {instance.status}")
    else:
        print("No Linode instances found.")

except linode_api4.errors.ApiError as e:
    print(f"API Error: {e.status} - {e.json}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

This Python example initializes the client with the PAT, which is ideally loaded from an environment variable for security. The SDK then handles adding the token to the request headers automatically.

Security best practices

Adhering to security best practices is crucial when handling Linode API credentials to protect your cloud infrastructure from unauthorized access.

  • Least Privilege: Always grant the minimum necessary permissions (scopes) to your Personal Access Tokens and OAuth applications. A token with read_only access cannot accidentally (or maliciously) delete resources. Regularly review and adjust scopes as needs change.
  • Token Expiration: Set expiration dates for all Personal Access Tokens. Short-lived tokens reduce the window of opportunity for attackers if a token is compromised. For tokens that need to persist, consider implementing a rotation strategy.
  • Secure Storage: Never hardcode API keys or tokens directly into your source code. Store them in secure environment variables, dedicated secrets management services (e.g., HashiCorp Vault, AWS Secrets Manager, Azure Key Vault), or secure configuration files that are not committed to version control.
  • Environment Variables: For development and deployment, using environment variables (e.g., LINODE_CLI_TOKEN) is a common and effective way to manage API keys without exposing them in code.
  • Version Control Exclusion: Ensure that any files containing API keys or secrets are excluded from your version control system (e.g., using .gitignore for Git repositories).
  • Regular Audits: Periodically review your active API tokens and OAuth clients in the Linode Cloud Manager. Delete any tokens or clients that are no longer needed or whose purpose has changed significantly.
  • Monitor API Activity: Utilize Linode's logging and monitoring features to detect unusual API activity that might indicate a compromised token or unauthorized access.
  • Rate Limiting: Be aware of API rate limits to avoid denial-of-service issues. While not directly an authentication security measure, it's a critical operational security consideration.
  • HTTPS Only: The Linode API is only accessible over HTTPS. Always ensure your API requests use HTTPS to encrypt data in transit, preventing eavesdropping and man-in-the-middle attacks.
  • Revoke Compromised Tokens: If you suspect a Personal Access Token or OAuth Client Secret has been compromised, immediately revoke it in the Linode Cloud Manager to prevent further unauthorized access.