Authentication overview

Clarifai provides an artificial intelligence platform specializing in computer vision and machine learning. Access to its APIs and services requires authentication to ensure that only authorized users and applications can interact with models, manage data, and consume resources. The authentication process verifies the identity of the requesting entity and grants permissions based on the associated credentials. This mechanism is fundamental for maintaining the security and integrity of user data and intellectual property within the Clarifai ecosystem.

The primary method for authenticating with Clarifai's API is through the use of API keys. These keys act as unique identifiers and secret tokens, allowing applications to make authenticated requests. Each API key is associated with a specific Clarifai account and can be configured with varying levels of access permissions, enabling granular control over what an application can do within the platform. This approach is common in RESTful API designs, where stateless requests carry authentication information for each interaction.

Clarifai's authentication system is designed to support a wide range of use cases, from simple script-based integrations to complex enterprise applications. The platform offers comprehensive documentation and SDKs in multiple programming languages, including Python, Java, Node.js, Go, cURL, PHP, and C#, to facilitate secure integration. Developers can manage their API keys and monitor usage directly through the Clarifai portal.

Supported authentication methods

Clarifai primarily uses API keys for authenticating requests to its platform. This method involves including a unique key in the header or body of each API request. The API key serves as both an identifier and a secret, allowing the Clarifai API to verify the sender's identity and authorize the requested action. While API keys are the standard, Clarifai's platform infrastructure may internally leverage other security protocols, but for external developer interaction, the API key is the primary interface.

API Keys

API keys are long, alphanumeric strings generated within the Clarifai developer console. They are unique to each user or application and are used to control access to Clarifai's services. When making an API request, the API key is typically passed in an HTTP header, such as Authorization: Key YOUR_API_KEY, or as a query parameter. This method is straightforward to implement and manage, making it suitable for most application integrations.

The security level of an API key depends heavily on how it is managed and protected. Clarifai recommends treating API keys as sensitive credentials, similar to passwords. Misuse or exposure of an API key can lead to unauthorized access to your Clarifai account and resources. For more general information on API key security, refer to resources like the Cloudflare API Keys documentation.

When to use API Keys:

  • Server-side applications: Ideal for backend services where the API key can be securely stored and managed, shielded from client-side exposure.
  • Automated scripts: Suitable for scripts that interact with Clarifai APIs for data processing, model training, or deployment.
  • Internal tools: For applications used within an organization where access control can be tightly managed.

The following table summarizes the primary authentication method for Clarifai:

Method When to Use Security Level
API Key Server-side applications, automated scripts, internal tools where the key can be kept confidential. Medium to High (dependent on key management and protection)

Getting your credentials

To interact with the Clarifai API, you need to obtain an API key. This process is managed through the Clarifai portal and typically involves creating an account and generating a new key within your application settings.

Step-by-step guide to obtaining an API key:

  1. Create a Clarifai Account: If you don't already have one, sign up for a Clarifai account on the Clarifai website. Clarifai offers a Community Plan that includes a free tier for getting started.
  2. Navigate to the Clarifai Portal: Once logged in, access the Clarifai portal, which serves as your dashboard for managing applications, models, and API keys.
  3. Create an Application (if necessary): Clarifai organizes resources within applications. If you haven't already, create a new application within the portal. This application will serve as a container for your models and data, and your API key will be associated with it.
  4. Generate an API Key: Within your application settings in the Clarifai portal, locate the section for API keys. There, you will find an option to generate a new API key. Clarifai allows you to assign specific scopes or permissions to your API keys, controlling what actions can be performed with that key. It is a best practice to grant only the necessary permissions to each key. For detailed instructions on generating and managing API keys, refer to the Clarifai Authentication documentation.
  5. Securely Store Your Key: Once generated, copy your API key and store it securely. Avoid hardcoding API keys directly into your application's source code, especially for client-side applications. Instead, use environment variables, secret management services, or secure configuration files.

Remember that API keys are sensitive. Treat them with the same level of confidentiality as passwords. If an API key is compromised, Clarifai provides mechanisms within the portal to revoke or regenerate keys, limiting potential damage.

Authenticated request example

This example demonstrates how to make an authenticated request to the Clarifai API using a Python SDK. The example retrieves a list of models available in your Clarifai application. Before running this code, ensure you have installed the Clarifai Python SDK (pip install clarifai) and replaced YOUR_CLARIFAI_API_KEY with your actual API key.

The example uses the ClarifaiStub to initialize the client with the API key and then calls the ListModels method. The API key is passed during the client initialization, ensuring all subsequent requests made by that client instance are authenticated.

from clarifai.client.stub import ClarifaiStub
from clarifai.grpc.grpc.api import resources_pb2, service_pb2, status_pb2

# Replace with your actual Clarifai API Key
YOUR_CLARIFAI_API_KEY = "YOUR_CLARIFAI_API_KEY"

# Initialize the Clarifai client with your API key
stub = ClarifaiStub(api_key=YOUR_CLARIFAI_API_KEY)

def list_clarifai_models():
    print("Listing Clarifai models...")
    try:
        # Make an authenticated request to list models
        response = stub.ListModels(
            service_pb2.ListModelsRequest()
        )

        if response.status.code != status_pb2.SUCCESS:
            print(f"Error listing models: {response.status.description}")
            return

        print("Successfully listed models:")
        for model in response.models:
            print(f"- Model ID: {model.id}, Name: {model.name}")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    list_clarifai_models()

This Python example illustrates the basic structure for authenticating API calls with Clarifai. The Clarifai SDKs abstract much of the HTTP request and header management, allowing developers to focus on integrating AI functionalities. For cURL and other language examples, consult the Clarifai API Reference.

Security best practices

Securing your Clarifai API keys and interactions is crucial to prevent unauthorized access, protect your data, and manage your resource consumption. Adhering to security best practices helps mitigate common vulnerabilities associated with API key usage.

  • Treat API Keys as Secrets: Never hardcode API keys directly into client-side code (e.g., JavaScript in a web browser, mobile apps) where they can be easily extracted. Store them in environment variables, secure configuration files, or dedicated secret management services like AWS Secrets Manager or Google Secret Manager.
  • Use Environment Variables: For server-side applications, environment variables are a common and effective way to manage API keys. This keeps the keys out of your codebase and allows for easy rotation without code changes.
  • Implement Least Privilege: When generating API keys in the Clarifai portal, grant only the minimum necessary permissions (scopes) required for the application's functionality. Avoid using a master key with full access for all purposes. If a key with limited permissions is compromised, the potential damage is contained.
  • Regular Key Rotation: Periodically rotate your API keys. This practice reduces the window of opportunity for a compromised key to be exploited. Clarifai's portal allows you to easily revoke old keys and generate new ones.
  • Monitor API Usage: Regularly monitor your Clarifai API usage for any unusual activity. Spikes in requests or calls to unauthorized endpoints could indicate a compromised key. Clarifai's portal provides usage analytics to help with this.
  • Secure Communication (HTTPS): Always ensure that your applications communicate with the Clarifai API over HTTPS. This encrypts data in transit, protecting your API key and other sensitive information from interception. All Clarifai API endpoints are served over HTTPS by default.
  • Client-Side Proxies: If you must make API calls from a client-side application, consider routing requests through a secure backend proxy server. The proxy can add the API key to the request before forwarding it to Clarifai, keeping the key hidden from the client.
  • IP Whitelisting (if available): If Clarifai supports IP whitelisting for API keys, configure it to restrict API access only to known IP addresses or ranges from which your application operates. This adds another layer of security, preventing unauthorized access from unknown locations.
  • Error Handling: Implement robust error handling in your application to gracefully manage authentication failures. Avoid logging API keys or sensitive information in error messages.
  • Review Clarifai Security Documentation: Regularly consult the Clarifai security and authentication documentation for the latest recommendations and features related to securing your integration.