Welcome to the AI Worker API project! This project is designed to provide a serverless API for interacting with AI models using Cloudflare Workers. It leverages the Hono framework for building web applications and integrates with a D1 database for data storage.
- Features
- Getting Started
- API Endpoints
- Authentication
- Database Schema
- Usage Flow
- Testing
- Contributing
- License
- User registration and authentication
- API key management
- Chat completions using AI models
- CORS support for cross-origin requests
- Built with TypeScript for type safety
To get started with the project, follow these steps:
- Node.js (version >= 16.9.0)
- Wrangler CLI for deploying Cloudflare Workers
- A Cloudflare account
-
Clone the repository:
git clone https://github.com/gifflet/ai-worker-api-cloudflare.git cd ai-worker-api-cloudflare -
Install the dependencies:
npm install
-
Set up your environment variables. Create a
.envfile in the root directory and add your configuration:JWT_SECRET=your-secret-key -
Run the development server:
npm run dev
- POST
/register- Request body:
{ "email": "user@example.com", "password": "yourpassword" } - Response:
{ "message": "User created", "token": "your_jwt_token" }
- Request body:
- POST
/login- Request body:
{ "email": "user@example.com", "password": "yourpassword" } - Response:
{ "token": "your_jwt_token" }
- Request body:
- POST
/api-keys/create- Request body:
{ "name": "API Key Name" } - Response:
{ "key": "your_api_key", "message": "API key created successfully. Save this key as it won't be shown again." } - Authorization: Bearer token required in the
Authorizationheader.
- Request body:
- GET
/api-keys/list- Response:
[ { "id": "key_id", "name": "API Key Name", "active": true, "created_at": "timestamp" } ] - Authorization: Bearer token required in the
Authorizationheader.
- Response:
- POST
/api-keys/revoke/:keyId- Response:
{ "message": "API key revoked successfully" } - Authorization: Bearer token required in the
Authorizationheader.
- Response:
- POST
/chat/completions- Request body:
{ "messages": [ { "role": "user", "content": "Hello!" } ] } - Response:
{ "response": "AI response here" }
- Request body:
This API uses JWT for authentication. You need to include the token in the Authorization header as a Bearer token for protected routes.
Example:
Authorization: Bearer your_jwt_tokenThe project uses a D1 database with the following schema:
-
Users Table
id: TEXT PRIMARY KEYemail: TEXT UNIQUE NOT NULLpassword: TEXT NOT NULLcreated_at: DATETIME DEFAULT CURRENT_TIMESTAMP
-
API Keys Table
id: TEXT PRIMARY KEYuser_id: TEXT NOT NULLkey: TEXT UNIQUE NOT NULLname: TEXT NOT NULLactive: BOOLEAN DEFAULT truecreated_at: DATETIME DEFAULT CURRENT_TIMESTAMP
This section outlines the typical flow of using the AI Worker API, from user registration to making chat requests.
To start using the API, a user must first register. Send a POST request to the /register endpoint with the user's email and password.
Request:
curl -X POST http://localhost:8787/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "yourpassword"
}'Response:
{
"message": "User created",
"token": "your_jwt_token"
}After registration, the user can log in to obtain a JWT token. Send a POST request to the /login endpoint with the user's credentials.
Request:
curl -X POST http://localhost:8787/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "yourpassword"
}'Response:
{
"token": "your_jwt_token"
}Once logged in, the user can create an API key. Send a POST request to the /api-keys/create endpoint with a name for the key.
Request:
curl -X POST http://localhost:8787/api-keys/create \
-H "Authorization: Bearer your_jwt_token" \
-H "Content-Type: application/json" \
-d '{
"name": "API Key Name"
}'Response:
{
"key": "your_api_key",
"message": "API key created successfully. Save this key as it won't be shown again."
}With the API key, the user can now make chat requests. Send a POST request to the /chat/completions endpoint with the messages to be processed.
Request:
curl -X POST http://localhost:8787/chat/completions \
-H "Authorization: Bearer your_jwt_token" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'Response:
{
"response": "AI response here"
}If the user wants to revoke an API key, they can send a POST request to the /api-keys/revoke/:keyId endpoint.
Request:
curl -X POST http://localhost:8787/api-keys/revoke/keyId \
-H "Authorization: Bearer your_jwt_token"Response:
{
"message": "API key revoked successfully"
}This flow provides a comprehensive guide to using the AI Worker API, from user registration to making chat requests and managing API keys.
To run the tests, use the following command:
npm run testContributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature). - Make your changes and commit them (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.