Authentication overview

Accessing the GroqCloud API to utilize its LPU Inference Engine requires authentication to verify the identity of the client making the request. Groq primarily employs API keys as the mechanism for authenticating requests. This approach is common for cloud-based API services, providing a straightforward method for developers to secure their interactions with the platform. API keys function as a unique identifier and a secret token, which must be included with every API request to authorize access to Groq's services, such as model inference endpoints.

The authentication process involves generating an API key within the GroqCloud console and then including this key in the Authorization header of HTTP requests. This method aligns with standard practices for RESTful API authentication, ensuring that only authenticated and authorized applications can consume Groq's low-latency LLM inference capabilities. Groq's API is designed to be compatible with the OpenAI API specification, which simplifies integration for developers already familiar with similar large language model platforms. This compatibility extends to the authentication scheme, making the transition or integration process more seamless for developers.

The use of API keys provides a balance between ease of implementation and necessary security. Developers are responsible for safeguarding their API keys to prevent unauthorized access to their Groq accounts and resources. Groq's documentation provides specific guidance on how to manage and use these keys securely, emphasizing practices like environment variables and secret management tools to protect sensitive credentials. The platform's commitment to security is further underscored by its SOC 2 Type II compliance, indicating adherence to rigorous security standards for its operational processes and data handling practices.

Supported authentication methods

Groq's API primarily supports authentication via API keys. This is a standard and widely adopted method for securing access to web APIs. When an API key is used, it acts as both a unique identifier for the calling application or user and a secret token that verifies its authenticity.

API Key (Bearer Token)

  • Mechanism: The API key is passed as a Bearer token in the Authorization HTTP header. This is the most common and recommended method for authenticating with Groq.
  • Format: Authorization: Bearer YOUR_GROQ_API_KEY
  • When to use: Ideal for server-side applications, command-line tools, and client-side applications where the key can be securely managed and prevented from being exposed.
  • Security Considerations: Requires careful handling of the API key to prevent unauthorized access. Key rotation and restricted permissions are recommended.

Table: Groq Authentication Methods

Method Description When to Use Security Level
API Key (Bearer Token) A secret string passed in the Authorization header. Server-side applications, CLI tools, secure client-side contexts. Moderate to High (depends on key management)

While API keys are the primary method, developers should be aware of the broader landscape of authentication mechanisms for APIs. For instance, OAuth 2.0 is a common framework for delegated authorization, often used when third-party applications need limited access to a user's resources without exposing their credentials. However, for direct application-to-Groq service communication, a simple API key is sufficient and recommended by Groq for its ease of use and effectiveness.

Getting your credentials

To begin interacting with the GroqCloud API, you need to obtain an API key. This key serves as your primary credential for authenticating requests. The process for generating and managing your API keys is handled through the GroqCloud console.

  1. Create a GroqCloud Account: If you don't already have one, sign up for a GroqCloud account on the Groq homepage. This typically involves providing an email address and setting a password.
  2. Access the GroqCloud Console: Once your account is created and verified, log in to the GroqCloud console. This web-based interface is where you manage your projects, monitor usage, and generate API keys.
  3. Navigate to API Keys Section: Within the console, look for a section dedicated to API Keys or Developer Settings. The exact navigation path may vary slightly but is usually clearly labeled. Refer to the Groq documentation on getting started for the most current instructions.
  4. Generate a New API Key: Click on an option to create a new API key. You may be prompted to give your key a descriptive name to help you identify its purpose later.
  5. Copy Your API Key: After generation, your API key will be displayed. It is crucial to copy this key immediately and store it securely, as it will typically only be shown once. If you lose it, you will need to revoke it and generate a new one.
  6. Environment Variable Setup (Recommended): For application development, it is highly recommended to store your API key in an environment variable rather than hardcoding it directly into your application's source code. For example, you might set GROQ_API_KEY="sk_your_secret_key_here" in your development environment. This prevents accidental exposure of your key in version control systems.

Groq API keys are typically prefixed with sk_ to signify their secret nature. Always treat these keys as sensitive information, similar to passwords.

Authenticated request example

Once you have obtained your Groq API key, you can use it to authenticate your requests to the GroqCloud API. The examples below demonstrate how to make an authenticated call using both cURL and the Groq Python SDK, following the standard Bearer token authentication scheme.

cURL Example

This cURL command demonstrates a basic request to the chat completions endpoint, including your API key in the Authorization header.

curl -X POST https://api.groq.com/openai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $GROQ_API_KEY" \
  -d '{ "messages": [{"role": "user", "content": "Explain Groq's LPU."}], "model": "llama3-8b-8192" }'

In this example, $GROQ_API_KEY is an environment variable holding your actual Groq API key. Replace llama3-8b-8192 with your desired model if different. You can find more details on available models in the Groq API reference documentation.

Python SDK Example

Groq provides official SDKs for Python and JavaScript, which simplify the authentication process by abstracting the HTTP header management. You can install the Python SDK via pip:

pip install groq

Then, authenticate and make a request:

import os
from groq import Groq

# Ensure your GROQ_API_KEY is set as an environment variable
client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "What is the capital of France?",
        }
    ],
    model="llama3-8b-8192",
)

print(chat_completion.choices[0].message.content)

The Python SDK automatically picks up the GROQ_API_KEY environment variable if it's set, or you can pass it directly during client initialization (though environment variables are preferred for security). The Groq API reference provides comprehensive details on all available endpoints and parameters.

JavaScript SDK Example

First, install the JavaScript SDK:

npm install groq

Then, use it in your JavaScript application:

import Groq from "groq";

const groq = new Groq({
  apiKey: process.env.GROQ_API_KEY,
});

async function getGroqChatCompletion() {
  const chatCompletion = await groq.chat.completions.create({
    messages: [
      {
        role: "user",
        content: "How fast is Groq's LPU?",
      },
    ],
    model: "llama3-8b-8192",
  });

  console.log(chatCompletion.choices[0]?.message?.content || "");
}

getGroqChatCompletion();

Similar to the Python SDK, the JavaScript SDK expects the API key to be provided, ideally via an environment variable like GROQ_API_KEY.

Security best practices

Securing your Groq API keys is critical to prevent unauthorized usage, protect your account, and maintain the integrity of your applications. Adhering to these best practices will help mitigate common security risks associated with API key management.

  1. Never Hardcode API Keys: Directly embedding API keys into your source code is a significant security risk. These keys can be exposed if your code is publicly accessible (e.g., in a public Git repository). Instead, use environment variables, secret management services, or configuration files that are not committed to version control.
  2. Use Environment Variables: For server-side applications, storing API keys as environment variables (e.g., GROQ_API_KEY) is a standard and effective practice. This keeps the key out of your codebase and allows for easy rotation without code changes.
  3. Implement Server-Side Calls: Avoid making direct API calls from client-side code (e.g., JavaScript in a web browser or mobile apps) that exposes your API key. Instead, route all Groq API calls through your own backend server. Your server can then securely store and use the API key, acting as a proxy between your client application and the Groq API. This aligns with general recommendations for API security best practices from Google Developers, emphasizing server-side proxying.
  4. Restrict Key Permissions (if available): While Groq API keys currently provide broad access, always configure keys with the minimum necessary permissions if the platform introduces granular access controls in the future. This principle of least privilege limits the damage if a key is compromised.
  5. Regularly Rotate API Keys: Periodically generate new API keys and revoke old ones. This practice reduces the window of opportunity for a compromised key to be exploited. Groq allows you to manage and revoke keys within the GroqCloud console.
  6. Monitor Usage and Logs: Regularly review your Groq usage metrics and any available logs in the GroqCloud console. Unusual spikes in usage or unexpected activity could indicate a compromised key.
  7. Secure Your Development Environment: Ensure that your development machines and build pipelines are secure. Implement strong access controls, use firewalls, and keep software updated to prevent malware that could steal credentials.
  8. Use Secret Management Services: For complex deployments or enterprise environments, consider using dedicated secret management services like AWS Secrets Manager, Google Cloud Secret Manager, or HashiCorp Vault. These tools provide centralized, encrypted storage and controlled access to sensitive credentials.
  9. Enable Multi-Factor Authentication (MFA): Secure your GroqCloud account itself with MFA if available. This adds an extra layer of security to prevent unauthorized access to your console, even if your password is compromised.

By implementing these security measures, you can significantly reduce the risk of unauthorized access to your Groq API and protect your applications and data.