An open API service indexing awesome lists of open source software.

https://github.com/louisbrulenaudet/genai-api

Supercharge your applications using Cloudflare Workers and Gemini within minutes (and for free, up to hundreds of requests per day) ☁️✨
https://github.com/louisbrulenaudet/genai-api

ai ai-gateway ai-studio api apple-shortcuts cloudflare cloudflare-workers gemini genai generative-ai google-ai-studio hacktoberfest hono llm nlp shortcuts wrangler zod

Last synced: about 2 months ago
JSON representation

Supercharge your applications using Cloudflare Workers and Gemini within minutes (and for free, up to hundreds of requests per day) ☁️✨

Awesome Lists containing this project

README

          


Bodyboard Thumbnail

# Supercharge your applications using Cloudflare Worker and Gemini within minutes ☁️✨

[![Biome](https://img.shields.io/badge/lint-biome-blue?logo=biome)](https://biomejs.dev/)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](./LICENSE)
[![Known Vulnerabilities](https://snyk.io/test/github/louisbrulenaudet/genai-api/badge.svg)](https://snyk.io/test/github/louisbrulenaudet/genai-api)
[![Zod](https://img.shields.io/badge/validation-zod-blueviolet?logo=zod)](https://github.com/colinhacks/zod)

This TypeScript API built with [Hono](https://hono.dev/), [OpenAI](https://openai.com/), and deployed as a Cloudflare Worker aims originally to boost the performance of Apple's Shortcuts by providing a seamless integration with generative AI capabilities, allowing users to create more powerful and intelligent shortcuts and leveraging the full potential of information extraction provided by the Apple ecosystem. Nowadays, it can now be used to supercharge any application by providing a seamless integration with generative AI capabilities.

The objective of this repo is to provide a very simple and easy-to-use API for developers to integrate generative AI capabilities into their shortcuts in a minute. You just have to download this repository, slightly edit the configuration files, notably the `wrangler.jsonc` file, and run `make init` (eventually `make update`), then:

```bash
wrangler secret put GOOGLE_AI_STUDIO_API_KEY
```

Wrangler will prompt you to enter your API key, which will be securely stored as a secret environment variable in your Cloudflare Worker. If you don't have an API key yet, you can get one from the [Google AI Studio](https://aistudio.google.com/apikey).

Then, you need to put a bearer token for security reasons:

```bash
wrangler secret put BEARER_TOKEN
```

Once you have set up the secrets, you can deploy the API to Cloudflare Workers using:

```bash
make deploy
```

Here you go! Your API is now deployed and ready to use. You can test it by sending requests to the Cloudflare Workers URL provided in the output. The `completion` endpoint must be accessible pinging something like `https://your-cloudflare-worker-url.com/api/v1/completion`.

## API Usage

### Providers & Models

- **Providers:** `google-ai-studio`
- **Models:** `gemini-2.5-flash`, `gemini-2.5-flash-lite`, `gemini-2.0-flash`, `gemini-2.0-flash-lite`

### POST `/completion`

The API supports both text and image input (multimodal). You can send images (as data URLs) alongside text in your requests, enabling advanced use cases such as visual question answering, image captioning, and more.

Send a POST request to `/completion` with a JSON body:

```http
POST /completion
Content-Type: application/json
Authorization: Bearer your_token
X-API-Key: your_provider_specific_api_key

{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
]
}
],
"temperature": 0.7,
"model": "gemini-2.0-flash"
}
```

Or, with only text:

```http
POST /completion
Content-Type: application/json
Authorization: Bearer your_token
X-API-Key: your_provider_specific_api_key

{
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}
```

#### Request Parameters

- `messages` (array, required): List of message objects, each with:
- `role` (string, required): One of `"system"`, `"user"`, `"assistant"`, `"developer"`.
- `content` (string or array, required): Either a string (text) or an array of content blocks:
- Text block: `{ "type": "text", "text": "..." }`
- Image block: `{ "type": "image_url", "image_url": "" }`
- **Image must be a valid data URL.**
- `temperature` (number, optional): Sampling temperature (default 0.2, range 0–2).
- `provider` (string, optional): Provider name (default: `google-ai-studio`).
- `model` (string, optional): Model name (default: `gemini-2.5-flash`).
- `reasoning_effort` (string, optional): Reasoning effort level.

The response will be plain text with the model's completion.

Apple provide a helpful guide [here](https://support.apple.com/fr-fr/guide/shortcuts/apd58d46713f/ios) that you can follow to create your first shortcut using API calls.

**Authentication:**
- Always include the `Authorization` header with your Bearer token.
- You may also send an `X-API-Key` header to use a provider-specific key for requests, without needing to rotate secrets in environment variables.

Without the `Authorization` header, the request will fail.

### Development

If you want to contribute to the development of this API or simply run it locally, you can follow these steps:

1. Clone the repository:

```sh
git clone https://github.com/louisbrulenaudet/genai-api.git
cd genai-api
```

2. Install dependencies:

```sh
make init
```

3. Set up environment variables in `.dev.vars`:

```sh
CLOUDFLARE_AI_GATEWAY_BASE_URL=https://gateway.ai.cloudflare.com/v1
CLOUDFLARE_ACCOUNT_ID=your_account_id
CLOUDFLARE_AI_GATEWAY_ID=your_cloudflare_ai_gateway_id
GOOGLE_AI_STUDIO_API_KEY=your_api_key
BEARER_TOKEN=your_bearer_token
```

4. Start the development server:

```sh
make dev
```

The API runs locally on port 8788.

## Makefile Commands

| Command | Description |
|-----------|---------------------------------------------|
| init | Initialize the project |
| update | Update dependencies |
| dev | Run development server |
| deploy | Deploy the application |
| format | Format codebase with Biome |
| lint | Lint codebase with Biome |
| types | Generate TypeScript worker types |
| cloc | Count lines of code in src/ |

## Feedback
If you have any feedback, please reach out at [louisbrulenaudet@icloud.com](mailto:louisbrulenaudet@icloud.com).