Build a Zero-Cost Semantic Search Engine with Cloudflare Workers
Build a Zero-Cost Semantic Search Engine with Cloudflare Workers
This project provides a production-ready semantic search engine built entirely on Cloudflare's free-tier infrastructure.
The API follows OpenAPI specifications with full Swagger documentation available at /swagger.json, making it easy to integrate and use in your applications.
Before deployment, create a Vectorize index as described in step 4 below.
pnpm installwrangler login to login to your Cloudflare account in wranglerwrangler vectorize create semantic-search --dimensions=256 --metric=cosinewrangler deploy to publish the API to Cloudflare WorkersOnce deployed, set your writer API key via the API_KEY_WRITER secret (the legacy API_KEY binding still works and is treated as a writer key). Optionally create API_KEY_READER for search-only clients.
API_KEY_WRITER or the legacy API_KEY) are required for mutating endpoints such as POST /v1/documents and DELETE /v1/documents/:id.API_KEY_READER) may call read/search endpoints (GET /v1/documents/:id, POST /v1/search). Writer keys are also accepted on those routes.wrangler secret put API_KEY_READER).If a reader key invokes a write endpoint the API responds with:
{
"error": "Writer key required for this endpoint"
}The API provides the following endpoints for managing and searching documents:
curl -X POST "https://your-worker.workers.dev/v1/documents" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_WRITER_KEY" \
-d '{
"text": "This is a sample document for semantic search",
"id": "document-id",
"metadata": {
"source": "example",
"category": "documentation"
}
}'Response:
{
"id": "document-id"
}curl -X GET "https://your-worker.workers.dev/v1/documents/{document-id}" \
-H "Authorization: Bearer YOUR_ACCESS_KEY"Response:
{
"id": "document-id",
"text": "This is a sample document for semantic search",
"metadata": {
"source": "example",
"category": "documentation"
}
}curl -X DELETE "https://your-worker.workers.dev/v1/documents/{document-id}" \
-H "Authorization: Bearer YOUR_WRITER_KEY"Response:
{
"success": true
}curl -X POST "https://your-worker.workers.dev/v1/search" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_KEY" \
-d '{
"query": "sample document",
"limit": 10
}'Response:
{
"results": [
{
"id": "document-id",
"text": "This is a sample document for semantic search",
"metadata": {
"source": "example",
"category": "documentation"
},
"score": 0.95
}
]
}All endpoints require a Bearer token in the Authorization header. Replace YOUR_WRITER_KEY with a writer key for mutating routes and YOUR_ACCESS_KEY with either a reader or writer key for read/search routes.
wrangler dev to start a local instance of the API.http://localhost:8787/ in your browser to see the Swagger interface where you can try the endpoints.src/ folder will automatically trigger the server to reload, you only need to refresh the Swagger interface.This project is licensed under the Apache License 2.0. It is free for commercial use, but you need to include a link to https://semanticsearch.ai/ in your product.
No open issues yet, or sync has not completed.