Say you want to try several AI models in a WordPress plugin or a Laravel project: one from OpenAI, one from Anthropic, one open-source model. That usually means three accounts, three keys, three invoices and three different libraries. OpenRouter turns that into one account and one key, and it has free models that are enough for learning and prototyping.

This guide goes from creating an account to a real request in code. If you have never called an AI API before, get the background in your first AI API call with PHP.

What OpenRouter actually does#

OpenRouter is a gateway: it takes your request, forwards it to the provider of the model you picked, and returns the response. Three features make it useful:

  • One API for everything: the request format is OpenAI-compatible; switching models means changing one string.
  • Free models: free variants of some models, with request limits.
  • One bill: for paid models you buy credits, and usage across all models draws from them.

The trade-off is an extra intermediary between you and the provider, so your data also passes through its servers.

Step by step: from sign-up to first answer#

  1. Go to the OpenRouter site and sign up with email or one of the common account options.
  2. Before writing code, try the site's own chat: pick a free model and ask a few questions to get a feel for it.
  3. Open the models page and filter by price to show free models. Copy the model ID (such as provider/model-name:free); the :free suffix marks the free variant.
  4. Create an API key in the keys section. Give it a clear name (for example the project name), and if a credit limit option is available, set one for the key.
  5. Store the key in a .env file or a server environment variable, never in code.
  6. Send your first request (below).
  7. Check the activity view in the dashboard to see which model and provider each request went to.

First request with curl#

The endpoint is OpenAI-compatible. Replace the model ID with the one you copied in step 3:

Bash
curl https://openrouter.ai/api/v1/chat/completions \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "provider/model-name:free",
    "messages": [
      {"role": "user", "content": "Explain what a REST API is in two sentences."}
    ]
  }'

The response has the familiar choices[0].message.content structure.

The same request in PHP#

This simple function uses PHP's built-in cURL and needs no library:

PHP
<?php
function openrouter_chat(string $prompt, string $model): ?string
{
    $key = getenv('OPENROUTER_API_KEY');
    if (!$key) {
        return null;
    }

    $payload = json_encode([
        'model'    => $model,
        'messages' => [
            ['role' => 'user', 'content' => $prompt],
        ],
    ], JSON_UNESCAPED_UNICODE);

    $ch = curl_init('https://openrouter.ai/api/v1/chat/completions');
    curl_setopt_array($ch, [
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 60,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . $key,
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS     => $payload,
    ]);

    $body   = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($body === false || $status !== 200) {
        error_log('OpenRouter error: HTTP ' . $status . ' ' . (string) $body);
        return null;
    }

    $data = json_decode($body, true);
    return $data['choices'][0]['message']['content'] ?? null;
}

echo htmlspecialchars(
    openrouter_chat('Write one sentence about WordPress.', 'provider/model-name:free') ?? 'Error',
    ENT_QUOTES,
    'UTF-8'
);

If you use the official OpenAI library or any tool that accepts a "custom base URL", set the base URL to https://openrouter.ai/api/v1 and supply your OpenRouter key. Coding tools such as Cline usually list OpenRouter as a provider too.

Free model limits#

  • Request caps: free models are limited per minute and per day. Accounts that have bought some credits usually get a higher daily cap on free models as well; check the official page for the exact rule and numbers.
  • Less stability: free variants can be busy, slow, or return rate-limit errors (HTTP 429). Catch errors in your code and show a sensible message.
  • A changing list: a model that is free today may not be tomorrow. Keep the model ID in configuration, not hard-coded.
  • Privacy: some free providers may log requests. Review your account's privacy settings and don't send sensitive data.

Don't rely on a free model for a real product with users; it is excellent for testing and prototyping.

Practical tips#

  • Compare models quickly: send one fixed prompt to several models, changing only the ID. Quality varies a lot for non-English text, so this comparison is worth it.
  • Cheap model for simple work: send classification, data extraction and short summaries to a cheap or free model, and only hard tasks to a strong one. We show how to estimate costs in calculating and reducing AI API costs.
  • One key per project: if a key leaks, you revoke only that one, and each project's usage stays separate.
  • Tools and structured output: many models on OpenRouter support tool calling; learn the concept in function calling in AI APIs. Check each model's page for support.

Key security#

An OpenRouter key can reach every paid model, so a leak gets expensive. Never put the key in client-side JavaScript, a mobile app or a public repository; always send requests from your own server. See the full list in what never to give an AI.

Some services restrict access by country, and some hosting providers block outbound calls to AI APIs; test from your actual server.

Alternatives#

  • Ollama and LM Studio: run open-source models on your own machine without sending data anywhere; get the background in open-source AI models.
  • Hugging Face: access open models and run them through connected providers.
  • The direct ChatGPT or Claude API: if you only need one company's models, there is one less intermediary.

Summary#

  • OpenRouter gives you one account and one key for models from many companies.
  • Find free models with the price filter and the :free suffix.
  • The API is OpenAI-compatible; only the base URL, key and model name change.
  • Free models have request caps and less stability; they are best for testing.
  • Keep the key server-side and create a separate key for each project.