国内一直无法直连OPENAI API,近期xai的api也不支持国内直接访问了,不过可以通过vercel代理一下继续使用,代码如下:
首先创建一个仓库(建议新建一个private仓库),新建一个文件名:vercel.json
编辑保存:
{ "routes": [ { "src": "/", "dest": "/index.html" }, { "src": "/gemini/v1/(.*)", "dest": "https://generativelanguage.googleapis.com/v1/\" }, { "src": "/gemini/v1beta/(.*)", "dest": "https://generativelanguage.googleapis.com/v1beta/\" }, { "src": "/openai/v1/(.*)", "dest": "https://api.openai.com/v1/\" }, { "src": "/groq/openai/v1/(.*)", "dest": "https://api.groq.com/openai/v1/\" }, { "src": "/claude/v1/(.*)", "dest": "https://api.anthropic.com/v1/\" }, { "src": "/cohere/v1/(.*)", "dest": "https://api.cohere.ai/v1/\" }, { "src": "/xai/v1/(.*)", "dest": "https://api.x.ai/v1/\" } ], "regions": [ "hnd1" ] }
然后新创建一个index.html用来首页显示一个api说明,编辑:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>API Access文档</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px; } .container { max-width: 800px; margin: auto; background-color: #fff; border-radius: 12px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); padding: 20px; margin-top: 20px; } h1 { text-align: center; color: #333; } p { color: #666; line-height: 1.6; } .api-info { background-color: #e9ecef; border-radius: 8px; padding: 15px; margin-top: 20px; } .api-url { font-family: 'Courier New', Courier, monospace; background-color: #d1ecf1; border-radius: 8px; padding: 10px; margin-top: 10px; } .api-example { background-color: #e9ecef; border-radius: 8px; padding: 15px; margin-top: 20px; } .code-block { background-color: #f1f8ff; border-radius: 8px; padding: 10px; margin-top: 10px; overflow-x: auto; } button { background-color: #007bff; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; margin-top: 10px; } button:hover { background-color: #0056b3; } #articlesummary { border-radius: 12px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); padding: 16px; margin-top: 16px; background-color: #fff; } #summarycontent { margin-top: 10px; } </style> </head> <body> <div class="container"> <h1>api access<br> API帮助文档</h1> <div class="api-info"> <h2>API Access Support</h2> <p>openai</p> <p>gemini</p> <p>groq</p> <p>claude</p> <p>cohere</p> <p>xai</p> </div> <div class="api-info"> <h2>API Endpoint</h2> <div class="code-block"> <pre>host/theapi</pre> <pre>EXP:https://url/xai</pre> </div> </div> </div> </body> </html>
然后访问 vercel.com,导入这个仓库,无需任何配置,直接部署即可,部署完成后绑定你自己的域名就可以放在one api里玩耍了:
比如,如果是xai,则代理地址写:https://url/xai