Authentication overview

DeepAI provides access to its suite of machine learning models, including image generation, text generation, and image editing APIs, through a straightforward authentication mechanism. The core method for securing API requests involves using a unique, secret API key. This key identifies the calling application or user and links API usage to a specific DeepAI account for billing and rate limiting purposes. Proper handling and protection of this API key are essential for maintaining the security and integrity of your integrations.

The system is designed for ease of integration, allowing developers to quickly get started with generative AI tasks. When making requests, the API key is typically sent in an HTTP header, which is a common practice for API key authentication across various web services. This approach offers a balance between security and developer convenience, making it suitable for quick prototyping and integrating AI features into applications.

Understanding the fundamental principles of API key security, such as those outlined by the W3C's approach to API Keys, is important for developers working with DeepAI and similar services. API keys are long-lived credentials, which means their security relies heavily on correct implementation and storage practices within your application environment.

Supported authentication methods

DeepAI exclusively supports API Key authentication for accessing its machine learning model APIs. This method is standard for many public APIs due to its simplicity and effectiveness in identifying clients and managing resource access.

API Key Authentication

With API Key authentication, each request to the DeepAI API must include a unique secret key. This key acts as a digital credential, proving that the request originates from an authorized user or application. The key is typically passed in a custom HTTP header, ensuring it is present with every interaction.

How it works:

  1. You obtain a secret API key from your DeepAI account dashboard.
  2. For every API call, you include this key in a designated HTTP header (e.g., api-key or Authorization: Bearer style).
  3. DeepAI's servers validate the key to ensure it is authentic and active.
  4. Upon successful validation, the request is processed, and the associated usage is recorded against your account.

This method is suitable for server-side applications, backend services, and development environments where the API key can be securely stored and managed. For client-side applications or publicly accessible code, alternative strategies like proxying requests through a secure backend are generally recommended to prevent exposure of the API key.

Authentication Methods Table

Method When to Use Security Level DeepAI Support
API Key Server-side applications, backend services, quick prototyping Moderate (dependent on secure storage) Primary
OAuth 2.0 User authorization flows, third-party integrations requiring delegated access High Not Supported
Basic Auth Simple client-server authentication, legacy systems Low to Moderate (requires HTTPS) Not Supported

Getting your credentials

To access DeepAI's API endpoints, you must obtain an API key from your account dashboard. This key is your primary credential for all API interactions.

  1. Create a DeepAI Account: If you don't have one, navigate to the DeepAI homepage and sign up.
  2. Access Your Dashboard: Log in to your DeepAI account.
  3. Locate API Keys Section: On your dashboard, look for a section labeled 'API Keys' or similar. The exact location may vary slightly but is typically found under 'Account Settings' or a dedicated 'Developers' area. You can usually find direct links on the DeepAI API documentation page.
  4. Generate Your API Key: If you haven't generated a key before, there will be an option to create a new secret API key. DeepAI provides one secret API key per user account by default.
  5. Copy Your API Key: Once generated, copy the API key immediately. DeepAI generally displays the key only once upon creation for security reasons. If you lose it, you may need to generate a new one, invalidating the old key.

Your API key is a secret credential and should be treated with the same care as a password. It provides full access to your DeepAI account's API usage and associated billing.

Authenticated request example

This section demonstrates how to make an authenticated request to a DeepAI API endpoint using cURL and Python, two common methods for interacting with web APIs. We will use the Text Generation API as an example, specifically the endpoint for generating text.

Example with cURL

cURL is a command-line tool and library for transferring data with URLs. It's often used for testing API endpoints quickly.

curl -H "api-key: YOUR_API_KEY" \
     -X POST \
     -d "text=Generate a short story about a brave knight." \
     https://api.deepai.org/api/text-generator

In this example:

  • -H "api-key: YOUR_API_KEY": This sets the api-key HTTP header with your actual API key. Replace YOUR_API_KEY with the key obtained from your DeepAI dashboard.
  • -X POST: Specifies that this is an HTTP POST request, which is required for submitting data to the text generation API.
  • -d "text=Generate a short story about a brave knight.": This sends the payload data as form-urlencoded data, containing the prompt for the text generation model.
  • https://api.deepai.org/api/text-generator: This is the target endpoint for the Text Generation API.

Example with Python

Python is a popular language for scripting and building web applications. The requests library simplifies making HTTP requests.

import requests

DEEPAI_API_KEY = "YOUR_API_KEY"  # Replace with your actual DeepAI API key
API_URL = "https://api.deepai.org/api/text-generator"

headers = {
    "api-key": DEEPAI_API_KEY,
}

data = {
    "text": "Write a simple poem about a cat."
}

try:
    response = requests.post(API_URL, headers=headers, data=data)
    response.raise_for_status()  # Raise an HTTPError for bad responses (4xx or 5xx)
    print(response.json())
except requests.exceptions.HTTPError as err:
    print(f"HTTP error occurred: {err}")
except requests.exceptions.ConnectionError as err:
    print(f"Connection error occurred: {err}")
except requests.exceptions.Timeout as err:
    print(f"Timeout error occurred: {err}")
except requests.exceptions.RequestException as err:
    print(f"An unexpected error occurred: {err}")

In the Python example:

  • DEEPAI_API_KEY = "YOUR_API_KEY": Store your API key in a variable. It's best practice to load this from an environment variable rather than hardcoding it.
  • headers = {"api-key": DEEPAI_API_KEY}: A dictionary defining the HTTP headers, including the required api-key.
  • data = {"text": "Write a simple poem about a cat."}: A dictionary containing the payload for the request.
  • requests.post(API_URL, headers=headers, data=data): Makes the POST request to the API endpoint with the specified headers and data.
  • Error handling is included to catch common issues like network problems or API errors, which is crucial for robust applications.

For more detailed examples and specific model parameters, refer to the DeepAI model API documentation.

Security best practices

Securing your DeepAI API key is paramount to prevent unauthorized access, potential misuse of your account, and unexpected charges. Adhering to these best practices will help protect your credentials and maintain the integrity of your applications.

  1. Never Expose API Keys in Client-Side Code:
    • Do not embed your API key directly in client-side JavaScript, mobile applications, or any code that runs in a public environment.
    • If your client-side application needs to interact with DeepAI, proxy requests through a secure backend server that can store and manage the API key. The backend server acts as an intermediary, making the authenticated calls to DeepAI and returning results to the client without exposing the key.
  2. Store API Keys Securely:
    • Environment Variables: For server-side applications, store API keys in environment variables (e.g., DEEPAI_API_KEY). This keeps them out of your source code and configuration files.
    • Secret Management Services: For production environments, consider using dedicated secret management services like AWS Secrets Manager, Google Cloud Secret Manager, or Azure Key Vault. These services provide secure storage, rotation, and access control for sensitive credentials.
    • Configuration Files (with caution): If using configuration files, ensure they are not committed to version control systems (like Git) and have strict file system permissions. Use .env files for local development and ensure they are listed in .gitignore.
  3. Use HTTPS/TLS for All API Communication:
    • DeepAI's API endpoints automatically enforce HTTPS. Ensure your application always uses https:// in API URLs to encrypt data in transit and prevent eavesdropping.
    • This standard practice for web APIs helps protect the API key itself as it travels over the network, as noted by security guidelines for secure contexts in web development.
  4. Implement Rate Limiting and Monitoring:
    • Monitor your API usage regularly through your DeepAI dashboard.
    • Implement client-side rate limiting in your application to prevent accidental or malicious bursts of requests that could lead to unexpected costs or exceed DeepAI's service limits.
    • Set up alerts for unusual API activity or excessive usage to detect potential key compromise early.
  5. Rotate API Keys Periodically:
    • Although DeepAI does not enforce key rotation, it's a good security practice to periodically generate a new API key and replace the old one in your applications. This reduces the risk associated with a long-lived credential.
    • If you suspect your API key has been compromised, immediately generate a new key from your DeepAI dashboard, which will invalidate the old one. Update all your applications with the new key.
  6. Restrict Access to API Keys:
    • Limit who has access to your DeepAI API keys within your team or organization. Only individuals who explicitly need access to deploy or manage the integration should have it.
    • Apply the principle of least privilege to restrict access to systems where API keys are stored.

By following these best practices, you can significantly reduce the risk of unauthorized API access and maintain a secure integration with DeepAI's services.