banner
xiaole

xiaole

前端工程师 | Trying To Do Better
tg_channel
twitter
github
email

Use cloudflare to proxy api.openai.com

Recently, I worked on a ChatGPT project and wanted to share it with my family. However, since I couldn't directly access the OpenAI API in China, I needed to use a proxy for the official interface.

The method can be found in chatgptProxyAPI.

Using Cloudflare Pages#

  1. Open GitHub and create a repository. You can fill in any repository name.

image.png

  1. Create a new file.

image

Name the file _worker.js. If you want to proxy other addresses, you can directly modify TELEGRAPH_URL.

The content is:

const TELEGRAPH_URL = 'https://api.openai.com';


export default {
  async fetch(request, env) {
      const NewResponse = await handleRequest(request)
      return NewResponse
  },
};

async function handleRequest(request) {
  const url = new URL(request.url);
  const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
  url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
  const modifiedRequest = new Request(url.toString(), {
    headers: request.headers,
    method: request.method,
    body: request.body,
    redirect: 'follow'
  });
  const response = await fetch(modifiedRequest);
  const modifiedResponse = new Response(response.body, response);
  // Add response header to allow cross-origin access
  modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
  return modifiedResponse;
}

image.png

Then, commit the changes.

  1. Open Cloudflare and create a new pages, and connect it to your Git.

image

  1. Choose the repository you just created.
  2. Use the default configuration (no need to make any modifications, proceed to the next step).
  3. Wait for the deployment to complete, and return to the created pages page to see the address.

image.png

Usage:#

How to use it in my project

  1. Open https://power-chat.younglele.cn.
  2. Fill in https://the-domain-address-you-obtained-above/v1 in the OpenAI API Proxy setting.

image.png

  1. Save and you can start accessing the openai api without the need for a VPN.

There is another way to use Cloudflare Worker, but you need to have your own domain and bind it to Cloudflare. It is still recommended to use the pages method.

Using Cloudflare Worker#

  1. Create an application in Workers and Pages.

image.png

  1. Click on Create Worker.

image.png

  1. Modify the name and click Deploy (modify the code after deployment).

image.png

  1. Click on Edit Code.

image.png

Paste the following code into the code editor, click Save and Deploy.

const TELEGRAPH_URL = 'https://api.openai.com';

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url);
  const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
  url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
  const modifiedRequest = new Request(url.toString(), {
    headers: request.headers,
    method: request.method,
    body: request.body,
    redirect: 'follow'
  });
  const response = await fetch(modifiedRequest);
  const modifiedResponse = new Response(response.body, response);
  // Add response header to allow cross-origin access
  modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
  return modifiedResponse;
}

image.png

After successful deployment, return to the created workers page.

After doing this, you may find that it is still not working because workers.dev is still blocked. In this case, you need to bind a domain.

image.png

However, this custom domain currently only supports domains that are active on Cloudflare. Click on Websites on the left and follow the steps to add your own domain or purchase one on Cloudflare.

image.png

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.