A serverless API using Cloudflare Workers and Firestore to generate AI-based travel itineraries with the OpenAI API.
git clone https://github.com/<your-username>/<your-repo>.git
cd <your-repo>npm installCreate a .env file in the root directory with:
OPENAI_API_KEY=your-openai-api-key
FIREBASE_PROJECT_ID=your-firestore-project-id
FIREBASE_CLIENT_EMAIL=your-firebase-service-account-email
FIREBASE_PRIVATE_KEY="your-firebase-private-key"Note: You can get these values from your Firebase Service Account JSON file in the Google Cloud Console.
- In the Firebase Console, create a Firestore database (Native mode).
- Create a collection called
itineraries. - Give your Firebase service account read/write access.
- Add the credentials from your service account to your worker using environment variables.
npx wrangler login
npx wrangler publishStak-task/
│
├── src/
│ ├── index.js <-- main worker logic
│ ├── firebase.js <-- Firestore access & token management
│ ├── routes.js <-- request routing / endpoint handlers
│ └── utils.js <-- helper functions (optional)
│
├── wrangler.toml <-- Cloudflare Worker configuration
├── package.json <-- dependencies & scripts
├── .env.example <-- example environment variables
└── README.md <-- project documentation
curl -X POST "https://stak-task.mbagher.workers.dev/itinerary" -H "Content-Type: application/json" -d "{\"destination\":\"Rasht, Iran\", \"durationDays\":5}"curl -X POST "https://stak-task.mbagher.workers.dev/itinerary" -H "Content-Type: application/json" -d '{"destination":"Rasht, Iran", "durationDays":5}'Example Response
{
"jobId": "uuid-generated-id"
}Status: 202 Accepted
curl -X GET "https://stak-task.mbagher.workers.dev/itinerary/<jobId>"curl -X GET "https://stak-task.mbagher.workers.dev/itinerary/<jobId>"Example Response
{
"status": "completed",
"destination": "Paris, France",
"durationDays": 3,
"createdAt": "Firestore Timestamp",
"completedAt": "Firestore Timestamp",
"itinerary": [
{
"day": 1,
"theme": "Historical Paris",
"activities": [
{
"time": "Morning",
"description": "Visit the Louvre Museum. Pre-book tickets to avoid queues.",
"location": "Louvre Museum"
}
]
}
],
"error": null
}