This Cloudflare Worker script acts as a proxy for the Claude AI API, utilizing Google Cloud Platform (GCP) for authentication.
- 🔄 Proxies requests to Claude AI API
- 🔑 Handles OAuth 2.0 token refresh with GCP
- 🛡️ Implements API key validation
- 🌐 Supports CORS for cross-origin requests
- ⚖️ Load balancing between different GCP regions
- Log in to your Cloudflare dashboard.
- Navigate to Workers & Pages > Create application > Create Worker.
- Give your worker a name and click "Deploy".
- In the editor that opens, replace all the code with the contents of the
worker.js
file from this repository. - Click "Save and deploy".
Set up the following environment variables in your Cloudflare Worker:
PROJECT_ID
: Your GCP project IDCLIENT_ID
: Your GCP OAuth 2.0 client IDCLIENT_SECRET
: Your GCP OAuth 2.0 client secretREFRESH_TOKEN
: Your GCP OAuth 2.0 refresh tokenAPI_KEY
: Your chosen API key for this worker
To obtain the necessary GCP credentials, use the following steps in Google Cloud Shell:
- Open Google Cloud Shell for your project.
- Run the following commands:
gcloud auth application-default login
cat /tmp/tmp.*/application_default_credentials.json
To set environment variables:
- Go to your Worker's settings.
- Click on the "Variables" tab.
- Add each variable under "Environment Variables".
To use this worker, send requests to your worker's URL with the following headers:
x-api-key
: Your API key (set in the environment variables)- Other headers as required by the Claude AI API
The worker will handle authentication and forward your request to the Claude AI API.
- Keep your
API_KEY
secret and secure. - Regularly rotate your GCP credentials and update the environment variables.
- Monitor your worker's usage to detect any unusual activity.
This project is MIT licensed. 本项目来源:LinuxDO
If you have any questions or feedback, please open an issue in this repository.