跳转到内容
搜索文档

OpenRouter

最后更新 查看 MarkdownAgent 设置

OpenRouter 是一个平台,提供用于访问和使用大型语言模型(LLM)的统一接口。

端点

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter

URL 结构

OpenRouter 发送请求时,将当前使用的 URL 中的 https://openrouter.ai/api/v1/chat/completions 替换为 https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter/chat/completions

前提条件

向 OpenRouter 发送请求时,请确保具备以下条件:

  • 你的 AI Gateway Account ID。
  • 你的 AI Gateway gateway 名称。
  • 有效的 OpenRouter API 令牌或原始模型提供商的令牌。
  • 要使用的 OpenRouter 模型名称。

示例

cURL

Requestbash
curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter/v1/chat/completions \
 --header 'content-type: application/json' \
 --header 'Authorization: Bearer OPENROUTER_TOKEN' \
 --data '{
    "model": "openai/gpt-5-mini",
    "messages": [
        {
            "role": "user",
            "content": "What is Cloudflare?"
        }
    ]
}'

使用 OpenAI SDK 与 JavaScript

如果你使用 OpenAI SDK 与 JavaScript,可以这样设置端点:

JavaScriptjs
import OpenAI from "openai";

const openai = new OpenAI({
	apiKey: env.OPENROUTER_TOKEN,
	baseURL:
		"https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter",
});

try {
	const chatCompletion = await openai.chat.completions.create({
		model: "openai/gpt-5-mini",
		messages: [{ role: "user", content: "What is Cloudflare?" }],
	});

	const response = chatCompletion.choices[0].message;

	return new Response(JSON.stringify(response));
} catch (e) {
	return new Response(e);
}

这篇文档对您有帮助吗?