How to Get API Keys for AI Services Used in AiToEarn

AiToEarn consumes API keys for OpenAI, Anthropic, Volcengine, xAI Grok, and Google Gemini through environment variables defined in project/aitoearn-backend/apps/aitoearn-ai/config/config.js, failing fast if required credentials are missing.

The open-source AiToEarn platform aggregates multiple large language model providers to power its content generation agents. Configuring API keys for AI services used in AiToEarn involves obtaining credentials from each provider and mapping them to specific environment variables that the Node.js backend reads at startup according to project/aitoearn-backend/apps/aitoearn-ai/config/config.js (lines 24-31).

Supported AI Providers and Environment Variables

The backend configuration unifies access to five major AI services. The application expects the following environment variables at runtime:

  • OPENAI_API_KEY – For OpenAI GPT models
  • ANTHROPIC_API_KEY – For Anthropic Claude models
  • VOLCENGINE_API_KEY – For ByteDance Volcengine models
  • GROK_API_KEY – For xAI Grok models
  • GEMINI_API_KEY – For Google Gemini single-key access
  • GEMINI_KEY_PAIRS – JSON-encoded mapping of project IDs to keys for multi-project Gemini access

These values are injected via Docker Compose, .env files, or Kubernetes secrets.

Obtaining API Keys from Each Provider

OpenAI

Sign up at the OpenAI Platform, navigate to the API keys section, and generate a new key. Copy the key starting with sk- for use in your environment variables.

Anthropic Claude

Register for an account at the Anthropic Console. Create a new API key starting with sk-ant and store it securely, as Anthropic only displays it once upon creation.

Volcengine

Apply for a developer account on the Volcengine Portal, enable the "AI Large Model" service, and retrieve your API key from the service management dashboard.

xAI Grok

Join the xAI developer program at x.ai and navigate to the developer dashboard to generate a Grok API key starting with grok-.

Google Gemini

Visit the Google Cloud Console, enable the Gemini API, and create an API key. For multi-project setups, AiToEarn expects a JSON object in GEMINI_KEY_PAIRS mapping project IDs to keys, parsed in lines 46-57 of the configuration file.

Configuring Keys in the Backend

The AiToEarn backend validates the presence of required keys during initialization. In project/aitoearn-backend/apps/aitoearn-ai/config/config.js (lines 46-49), the application throws an explicit error if GEMINI_KEY_PAIRS is undefined:

function parseGeminiKeyPairs() {
  if (!process.env.GEMINI_KEY_PAIRS) {
    throw new Error('GEMINI_KEY_PAIRS 环境变量必须配置');
  }
  return JSON.parse(process.env.GEMINI_KEY_PAIRS);
}

For standard single-key providers, the configuration maps environment variables directly to client settings (lines 98-112):

// project/aitoearn-backend/apps/aitoearn-ai/config/config.js
const { OPENAI_API_KEY, OPENAI_BASE_URL } = process.env;

module.exports = {
  ai: {
    openai: {
      baseUrl: OPENAI_BASE_URL,
      apiKey: OPENAI_API_KEY,
    },
    anthropic: {
      apiKey: process.env.ANTHROPIC_API_KEY,
    },
    volcengine: {
      apiKey: process.env.VOLCENGINE_API_KEY,
    },
    grok: {
      apiKey: process.env.GROK_API_KEY,
    },
    gemini: {
      apiKey: process.env.GEMINI_API_KEY,
      keyPairs: parseGeminiKeyPairs(),
    },
  },
};

Usage Example with OpenAI

Once configured, the application initializes the OpenAI client using the credentials from the config object:

const { Configuration, OpenAIApi } = require('openai');
const cfg = require('../config/config');

const openai = new OpenAIApi(
  new Configuration({
    apiKey: cfg.ai.openai.apiKey,
    basePath: cfg.ai.openai.baseUrl,
  })
);

async function generateContent(prompt) {
  const resp = await openai.createChatCompletion({
    model: 'gpt-4o-mini',
    messages: [{ role: 'user', content: prompt }],
  });
  return resp.data.choices[0].message.content;
}

Note that the AiToEarn platform also requires a separate RELAY_API_KEY defined in project/aitoearn-backend/apps/aitoearn-server/config/config.js for its own relay service authentication, as documented in the README_EN.md (lines 40-48).

Summary

  • Five AI providers are supported: OpenAI, Anthropic, Volcengine, xAI Grok, and Google Gemini
  • Environment variables (OPENAI_API_KEY, ANTHROPIC_API_KEY, VOLCENGINE_API_KEY, GROK_API_KEY, GEMINI_API_KEY, GEMINI_KEY_PAIRS) are defined in project/aitoearn-backend/apps/aitoearn-ai/config/config.js
  • GEMINI_KEY_PAIRS requires a JSON string format and throws an error if missing during startup
  • Runtime injection occurs via Docker Compose, .env files, or Kubernetes secrets
  • Platform key (RELAY_API_KEY) is configured separately in the server module for AiToEarn's relay authentication

Frequently Asked Questions

Where do I set the API keys when deploying AiToEarn with Docker?

Create a .env file in the project root or modify the docker-compose.yml environment section to include all required variables. The backend reads these at container startup via process.env, eliminating the need to hardcode credentials in the source files.

What happens if I forget to provide the GEMINI_KEY_PAIRS variable?

The application will fail to start. According to lines 46-49 in project/aitoearn-backend/apps/aitoearn-ai/config/config.js, the parseGeminiKeyPairs() function explicitly throws Error: GEMINI_KEY_PAIRS 环境变量必须配置 if the environment variable is undefined, preventing silent authentication failures.

Can I configure multiple Gemini projects with different API keys?

Yes. Set the GEMINI_KEY_PAIRS environment variable to a JSON object mapping project IDs to their respective keys, such as {"project-a":"key-1","project-b":"key-2"}. The configuration parser handles this mapping to route requests to the appropriate project context.

How do I rotate an API key without downtime?

Update the environment variable in your container orchestration platform (Docker Compose, Kubernetes, etc.) and restart the AiToEarn backend services. Since keys are read at startup rather than at request time, the new credentials take effect immediately upon restart with no caching issues.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →