Skip to content

Instantly share code, notes, and snippets.

@paicha
Last active July 2, 2024 07:06
Show Gist options
  • Save paicha/d26e48a0288df22c3fad12eb9547ef94 to your computer and use it in GitHub Desktop.
Save paicha/d26e48a0288df22c3fad12eb9547ef94 to your computer and use it in GitHub Desktop.
Create an OpenAI API proxy using only 10 lines of code on Cloudflare Worker.
async function handleRequest(request) {
const url = new URL(request.url)
url.host = "api.openai.com"
return fetch(url, { headers: request.headers, method: request.method, body: request.body })
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request))
})
@paicha
Copy link
Author

paicha commented Apr 4, 2023

Q: How to deploy a Cloudflare Worker in the Cloudflare dashboard

To deploy a Cloudflare Worker in the Cloudflare dashboard, follow these steps:

Log in to your Cloudflare account and navigate to the Workers section in the left-hand menu.
Click the "Create a Worker" button.
Enter a name for your worker and paste in the code for your worker.
Click the "Save and Deploy" button to deploy your worker.
Once you have deployed your worker, you can configure the routes that trigger it using the "Routes" tab in the Workers section. You can also view logs and analytics for your worker by navigating to the "Usage" tab.

Note that in order to use Cloudflare Workers, you will need a Cloudflare account and a subscription to the Workers service. There are various pricing plans available, depending on your usage needs.

@paicha
Copy link
Author

paicha commented Apr 4, 2023

Q: 用中文再回答一遍

要在 Cloudflare 仪表盘中部署 Cloudflare Worker,请按照以下步骤操作:

登录 Cloudflare 账户,并导航到左侧菜单中的 Workers 部分。
单击“创建一个 Worker”按钮。
为您的 Worker 输入名称并粘贴代码。
单击“保存并部署”按钮以部署您的 Worker。
部署完 Worker 后,您可以使用 Workers 部分中的“路由”选项卡配置触发它的路由。您还可以通过导航到“用量”选项卡查看您的 Worker 的日志和分析数据。

请注意,为了使用 Cloudflare Workers,您需要 Cloudflare 帐户和 Workers 服务的订阅。根据您的使用需求,有各种不同的定价计划可供选择。

@MOHU9527
Copy link

1024赞

@TanyuuKaribusa
Copy link

非常感谢您的内容,设置成功了!举手提问:适用于 Claude 么?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment