FREE AI INDEX / 统一产品界面我的收藏提交资源 ↗

Free AI Index / 从零开始Getting started / OpenRouter

FIRST API CALL / openrouter

OpenRouter:跑通第一次 API 调用Make your first OpenRouter API call

先确认注册与免费条件,再按步骤获取密钥、安装依赖并验证返回结果。教程只使用环境变量读取密钥。Check signup and free terms first, then create a key, install dependencies and validate the response. Examples read credentials from environment variables.

官方文档复核Official docs reviewed: 2026-10-07独立 API 实测Independent API test: 尚未记录Not recorded

注册与网络前提Signup and network requirements

网络 / 地区
国际服务;大陆账号注册与 API 连通性未由本教程实测,先在本人网络环境确认。
账号
OpenRouter 账号和 API Key;免费模型标识及限额可能变化。
手机号
官方 Quickstart 未说明统一手机号要求;以注册页面提示为准。
实名认证
官方 Quickstart 未说明统一实名认证要求;以账号页面提示为准。
信用卡 / 付款方式
免费模型和付费模型走同一平台;付款方式要求依账户和所选路由而定,当前未确认。

这些条件会因账号、地区和产品计划而变化。标注“待核验”表示 FreeLLM 尚未从当前链接的官方资料确认;不要据此推断为不需要。These requirements can vary by account, region and plan. “Unverified” means FreeLLM has not confirmed the condition from the linked official sources; it does not mean the requirement is absent.

获取并安全设置 API KeyGet and safely set your API key

在终端设置密钥Set the key in your terminal

以下命令只为当前终端会话设置环境变量。请在自己的电脑上把尖括号内容替换成真实密钥,不要把密钥发给 FreeLLM 或粘贴到公开代码。These commands set a variable for the current terminal session. Replace the angle-bracket text with your key on your own device; never send it to FreeLLM or commit it to source code.

Windows PowerShellWindows PowerShell

$env:OPENROUTER_API_KEY = "<在此粘贴本地 API Key>"

macOS / LinuxmacOS / Linux

export OPENROUTER_API_KEY="<在此粘贴本地 API Key>"

FreeLLM 资源记录FreeLLM resource records

详细操作步骤Detailed operation paths

每条路径都拆成前置条件、步骤、可复制命令和验证动作;免费条件仍以官方页面实时状态为准。curl 命令按 macOS / Linux shell 编写,Windows 用户可使用同页的 Python 或 Node.js 示例。Each path includes prerequisites, steps, copyable commands and a validation action; free terms still follow the provider's live official policy. The curl examples use a macOS/Linux shell; Windows users can run the Python or Node.js examples on this page.

调用免费模型

api

EndpointEndpoint
https://openrouter.ai/api/v1
认证Authentication
Authorization: Bearer API key
额度与限制Limits

调用额度与限流按提供商当前控制台和官方文档为准;免费条件可能按模型、账号和地区变化。

前置条件Prerequisites

  • 已注册 OpenRouterOpenRouter
  • 已创建 API keyAPI key
  • 已从模型页确认当前免费模型 IDID

操作步骤Steps

  1. 创建 API key

    进入 OpenRouter Keys 页面创建密钥,并将它放入 OPENROUTER_API_KEY 环境变量。

  2. 选择免费模型

    在官方 Models 页面筛选价格为 0 的模型;免费标记和可用性会变化,不要长期写死目录中的模型。

    操作Action:打开 https://openrouter.ai/models?max_price=0

  3. 发送 OpenAI 兼容请求

    将 model 替换成模型页显示的完整 ID。

    可复制命令Copyable command
    export OPENROUTER_API_KEY="你的 Key"
    curl https://openrouter.ai/api/v1/chat/completions -H "Authorization: Bearer $OPENROUTER_API_KEY" -H 'Content-Type: application/json' --data '{"model":"openai/gpt-oss-20b:free","messages":[{"role":"user","content":"Hello"}]}'

    预期结果Expected:响应包含 choices[0].message.content。

  4. Python SDK 示例

    安装 OpenAI SDK;在运行环境设置 OPENROUTER_API_KEY,示例代码通过环境变量读取密钥。

    可复制命令Copyable command
    python -m pip install -U openai
    
    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.environ["OPENROUTER_API_KEY"],
        base_url="https://openrouter.ai/api/v1",
    )
    response = client.chat.completions.create(
        model="openai/gpt-oss-20b:free",
        messages=[{"role": "user", "content": "用一句话解释 API 是什么。"}],
    )
    print(response.choices[0].message.content)

    预期结果Expected:终端打印模型生成的回复;再到提供商控制台确认请求状态和额度扣减。

  5. Node.js SDK 示例

    安装 OpenAI SDK;在运行环境设置 OPENROUTER_API_KEY,示例代码通过环境变量读取密钥。

    可复制命令Copyable command
    npm install openai
    
    import OpenAI from "openai";
    
    const client = new OpenAI({
      apiKey: process.env.OPENROUTER_API_KEY,
      baseURL: "https://openrouter.ai/api/v1",
    });
    const response = await client.chat.completions.create({
      model: "openai/gpt-oss-20b:free",
      messages: [{ role: "user", content: "用一句话解释 API 是什么。" }],
    });
    console.log(response.choices[0].message.content);

    预期结果Expected:Node.js 打印模型生成的回复;再到提供商控制台确认请求状态和额度扣减。

验证动作Validation

GET /api/v1/models 返回 HTTP 200;随后聊天请求返回 choices 数组。

常见问题Common issues
  • 401:检查 API Key、环境变量和认证 Header。401: API Key、 Header。
  • 404:从官方模型清单复制当前模型 ID,并确认 endpoint 与账号地域一致。404: ID, endpoint 。
  • 429:查看当前限流和配额重置说明,降低请求频率后再重试。429: , 。

官方来源Official sources