{"id":30387521,"url":"https://github.com/gifflet/ai-worker-api-cloudflare","last_synced_at":"2026-05-17T09:31:48.316Z","repository":{"id":305780830,"uuid":"898794014","full_name":"gifflet/ai-worker-api-cloudflare","owner":"gifflet","description":"AI Worker API built with Cloudflare Workers, TypeScript, and Hono framework. Features JWT authentication, API key management, and AI chat completions using D1 Database.","archived":false,"fork":false,"pushed_at":"2024-12-05T03:38:06.000Z","size":194,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-29T18:08:26.082Z","etag":null,"topics":["ai-integration","cloudflare-workers","d1-database","hono","jwt","serverless","typescript"],"latest_commit_sha":null,"homepage":"https://ai-worker-api-cloudflare.sousaguilherme89.workers.dev","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gifflet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-12-05T03:37:51.000Z","updated_at":"2024-12-23T19:26:04.000Z","dependencies_parsed_at":"2025-07-22T01:59:12.921Z","dependency_job_id":"37674aa4-26af-4502-bbd3-de47efa06e98","html_url":"https://github.com/gifflet/ai-worker-api-cloudflare","commit_stats":null,"previous_names":["gifflet/ai-worker-api-cloudflare"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gifflet/ai-worker-api-cloudflare","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gifflet%2Fai-worker-api-cloudflare","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gifflet%2Fai-worker-api-cloudflare/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gifflet%2Fai-worker-api-cloudflare/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gifflet%2Fai-worker-api-cloudflare/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gifflet","download_url":"https://codeload.github.com/gifflet/ai-worker-api-cloudflare/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gifflet%2Fai-worker-api-cloudflare/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33133486,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T09:28:26.183Z","status":"ssl_error","status_checked_at":"2026-05-17T09:27:52.702Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai-integration","cloudflare-workers","d1-database","hono","jwt","serverless","typescript"],"created_at":"2025-08-21T05:12:41.401Z","updated_at":"2026-05-17T09:31:48.310Z","avatar_url":"https://github.com/gifflet.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AI Worker API on Cloudflare\n\nWelcome 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.\n\n## Table of Contents\n\n- [Features](#features)\n- [Getting Started](#getting-started)\n- [API Endpoints](#api-endpoints)\n- [Authentication](#authentication)\n- [Database Schema](#database-schema)\n- [Usage Flow](#usage-flow)\n- [Testing](#testing)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Features\n\n- User registration and authentication\n- API key management\n- Chat completions using AI models\n- CORS support for cross-origin requests\n- Built with TypeScript for type safety\n\n## Getting Started\n\nTo get started with the project, follow these steps:\n\n### Prerequisites\n\n- Node.js (version \u003e= 16.9.0)\n- Wrangler CLI for deploying Cloudflare Workers\n- A Cloudflare account\n\n### Installation\n\n1. Clone the repository:\n\n   ```bash\n   git clone https://github.com/gifflet/ai-worker-api-cloudflare.git\n   cd ai-worker-api-cloudflare\n   ```\n\n2. Install the dependencies:\n\n   ```bash\n   npm install\n   ```\n\n3. Set up your environment variables. Create a `.env` file in the root directory and add your configuration:\n\n   ```plaintext\n   JWT_SECRET=your-secret-key\n   ```\n\n4. Run the development server:\n\n   ```bash\n   npm run dev\n   ```\n\n## API Endpoints\n\n### User Registration\n\n- **POST** `/register`\n  - Request body: `{ \"email\": \"user@example.com\", \"password\": \"yourpassword\" }`\n  - Response: `{ \"message\": \"User created\", \"token\": \"your_jwt_token\" }`\n\n### User Login\n\n- **POST** `/login`\n  - Request body: `{ \"email\": \"user@example.com\", \"password\": \"yourpassword\" }`\n  - Response: `{ \"token\": \"your_jwt_token\" }`\n\n### Create API Key\n\n- **POST** `/api-keys/create`\n  - Request body: `{ \"name\": \"API Key Name\" }`\n  - Response: `{ \"key\": \"your_api_key\", \"message\": \"API key created successfully. Save this key as it won't be shown again.\" }`\n  - **Authorization**: Bearer token required in the `Authorization` header.\n\n### List API Keys\n\n- **GET** `/api-keys/list`\n  - Response: `[ { \"id\": \"key_id\", \"name\": \"API Key Name\", \"active\": true, \"created_at\": \"timestamp\" } ]`\n  - **Authorization**: Bearer token required in the `Authorization` header.\n\n### Revoking API Key\n\n- **POST** `/api-keys/revoke/:keyId`\n  - Response: `{ \"message\": \"API key revoked successfully\" }`\n  - **Authorization**: Bearer token required in the `Authorization` header.\n\n### Chat Completions\n\n- **POST** `/chat/completions`\n  - Request body: `{ \"messages\": [ { \"role\": \"user\", \"content\": \"Hello!\" } ] }`\n  - Response: `{ \"response\": \"AI response here\" }`\n\n## Authentication\n\nThis API uses JWT for authentication. You need to include the token in the `Authorization` header as a Bearer token for protected routes.\n\nExample:\n\n```http\nAuthorization: Bearer your_jwt_token\n```\n\n## Database Schema\n\nThe project uses a D1 database with the following schema:\n\n- **Users Table**\n  - `id`: TEXT PRIMARY KEY\n  - `email`: TEXT UNIQUE NOT NULL\n  - `password`: TEXT NOT NULL\n  - `created_at`: DATETIME DEFAULT CURRENT_TIMESTAMP\n\n- **API Keys Table**\n  - `id`: TEXT PRIMARY KEY\n  - `user_id`: TEXT NOT NULL\n  - `key`: TEXT UNIQUE NOT NULL\n  - `name`: TEXT NOT NULL\n  - `active`: BOOLEAN DEFAULT true\n  - `created_at`: DATETIME DEFAULT CURRENT_TIMESTAMP\n\n## Usage Flow\n\nThis section outlines the typical flow of using the AI Worker API, from user registration to making chat requests.\n\n### 1. User Registration\n\nTo start using the API, a user must first register. Send a POST request to the `/register` endpoint with the user's email and password.\n\n**Request:**\n```bash\ncurl -X POST http://localhost:8787/register \\\n-H \"Content-Type: application/json\" \\\n-d '{\n  \"email\": \"user@example.com\",\n  \"password\": \"yourpassword\"\n}'\n```\n\n**Response:**\n```json\n{\n  \"message\": \"User created\",\n  \"token\": \"your_jwt_token\"\n}\n```\n\n### 2. User Login\n\nAfter registration, the user can log in to obtain a JWT token. Send a POST request to the `/login` endpoint with the user's credentials.\n\n**Request:**\n```bash\ncurl -X POST http://localhost:8787/login \\\n-H \"Content-Type: application/json\" \\\n-d '{\n  \"email\": \"user@example.com\",\n  \"password\": \"yourpassword\"\n}'\n```\n\n**Response:**\n```json\n{\n  \"token\": \"your_jwt_token\"\n}\n```\n\n### 3. Create API Key\n\nOnce 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.\n\n**Request:**\n```bash\ncurl -X POST http://localhost:8787/api-keys/create \\\n-H \"Authorization: Bearer your_jwt_token\" \\\n-H \"Content-Type: application/json\" \\\n-d '{\n  \"name\": \"API Key Name\"\n}'\n```\n\n**Response:**\n```json\n{\n  \"key\": \"your_api_key\",\n  \"message\": \"API key created successfully. Save this key as it won't be shown again.\"\n}\n```\n\n### 4. Making Chat Completions\n\nWith 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.\n\n**Request:**\n```bash\ncurl -X POST http://localhost:8787/chat/completions \\\n-H \"Authorization: Bearer your_jwt_token\" \\\n-H \"Content-Type: application/json\" \\\n-d '{\n  \"messages\": [\n    {\n      \"role\": \"user\",\n      \"content\": \"Hello!\"\n    }\n  ]\n}'\n```\n\n**Response:**\n```json\n{\n  \"response\": \"AI response here\"\n}\n```\n\n### 5. Revoking API Key\n\nIf the user wants to revoke an API key, they can send a POST request to the `/api-keys/revoke/:keyId` endpoint.\n\n**Request:**\n```bash\ncurl -X POST http://localhost:8787/api-keys/revoke/keyId \\\n-H \"Authorization: Bearer your_jwt_token\"\n```\n\n**Response:**\n```json\n{\n  \"message\": \"API key revoked successfully\"\n}\n```\n\n### Conclusion\n\nThis flow provides a comprehensive guide to using the AI Worker API, from user registration to making chat requests and managing API keys.\n\n## Testing\n\nTo run the tests, use the following command:\n\n```bash\nnpm run test\n```\n\n## Contributing\n\nContributions are welcome! Please follow these steps:\n\n1. Fork the repository.\n2. Create a new branch (`git checkout -b feature/YourFeature`).\n3. Make your changes and commit them (`git commit -m 'Add some feature'`).\n4. Push to the branch (`git push origin feature/YourFeature`).\n5. Open a pull request.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgifflet%2Fai-worker-api-cloudflare","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgifflet%2Fai-worker-api-cloudflare","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgifflet%2Fai-worker-api-cloudflare/lists"}