Skip to content

Provisioning & Initial Setup

This guide covers the one-time setup process for all required Cloudflare services.


1. Prerequisites

Before you begin, ensure you have the following:

  • Node.js: Version 18 or higher.
  • Wrangler CLI: The command-line interface for Cloudflare's developer platform.
# Install Wrangler globally
npm install -g wrangler@latest

# Log in to your Cloudflare account
wrangler login

# Verify your identity and note your Account ID
wrangler whoami
  • Cloudflare Account: Your account must have Workers, Vectorize, and AI Gateway enabled.
  • API Token (for CI/CD): For automated deployments, create a Cloudflare API token with the following permissions:
    • Account.Workers Scripts: Edit
    • Account.Vectorize: Write

2. Provisioning Checklist

Step 1: Create AI Gateway

The AI Gateway provides a unified endpoint for interacting with third-party LLMs, adding caching, rate limiting, and observability.

  1. In the Cloudflare dashboard, navigate to AI → AI Gateway.
  2. Click Create Gateway.
  3. Set the Gateway Name to labeeb-core.
  4. In the gateway's settings, enable Logging and Cache responses.
  5. Copy the OpenAI-compatible base URL. You will need this for the API configuration.
    https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/labeeb-core/openai
    

Step 2: Create Vectorize Indexes

Vectorize is Cloudflare's vector database. We need to create an index for each environment (dev, stage, prod).

Run for Each Environment

The following commands create the index and its required metadata fields. Repeat the create-metadata-index commands for labeeb-articles-stage and labeeb-articles.

Environment Index Name wrangler vectorize create ...
Development labeeb-articles-dev ... labeeb-articles-dev --dimensions=1024 --metric=cosine
Staging labeeb-articles-stage ... labeeb-articles-stage --dimensions=1024 --metric=cosine
Production labeeb-articles ... labeeb-articles --dimensions=1024 --metric=cosine
# Create the development index
wrangler vectorize create labeeb-articles-dev --dimensions=1024 --metric=cosine

# Add metadata indexes to the development index
wrangler vectorize create-metadata-index labeeb-articles-dev --property-name=lang --type=string
wrangler vectorize create-metadata-index labeeb-articles-dev --property-name=source --type=string
wrangler vectorize create-metadata-index labeeb-articles-dev --property-name=published_at_bucket --type=number

Step 3: Create Shared Secret

Create a secure secret to authenticate requests between the API service and the Cloudflare worker.

  1. Generate a strong secret:

    openssl rand -hex 32
    

  2. Set the secret for each worker environment:

    # Paste the generated secret when prompted
    wrangler secret put API_KEY             # For dev/default environment
    wrangler secret put API_KEY --env stage # For stage environment
    wrangler secret put API_KEY --env prod  # For prod environment
    

  3. Add the secret to the API's .env file: This key allows the API to securely communicate with the worker.

    CF_WORKER_BASE="https://cloudflare-stage.<your-subdomain>.workers.dev"
    CF_WORKER_API_KEY="<paste-the-same-secret-here>"