> ## Documentation Index
> Fetch the complete documentation index at: https://wemstudios.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Quickstart: Build Your First Blaze Worker in 5 Minutes

> Build and deploy your first Cloudflare Worker with Blaze in under 5 minutes. Install the package, define routes, and export the fetch handler.

By the end of this guide you'll have a running Cloudflare Worker that serves a JSON health-check endpoint and a typed D1 database route — deployed to Cloudflare's global network. You'll write the whole thing in TypeScript with full binding type safety from your first line of code.

<Steps>
  <Step title="Create a new Worker project">
    Scaffold a new Cloudflare Workers project using the official `create-cloudflare` CLI, then move into the project directory.

    ```bash theme={null}
    npm create cloudflare@latest my-worker
    cd my-worker
    ```

    When prompted, choose **"Hello World" Worker** and **TypeScript**. Skip Git initialisation if you prefer.
  </Step>

  <Step title="Install Blaze">
    Add Blaze to your project with your preferred package manager.

    <CodeGroup>
      ```bash npm theme={null}
      npm install blaze
      ```

      ```bash yarn theme={null}
      yarn add blaze
      ```

      ```bash pnpm theme={null}
      pnpm add blaze
      ```
    </CodeGroup>
  </Step>

  <Step title="Write your Worker">
    Replace the contents of `src/index.ts` with the following. This example defines an `Env` type that mirrors your `wrangler.toml` bindings, creates a typed app, registers two routes, and exports the Cloudflare Workers `fetch` handler.

    ```typescript src/index.ts theme={null}
    import { createApp } from 'blaze'

    // Mirror your wrangler.toml bindings exactly —
    // this type flows to every req.env call in your app.
    type Env = {
      DB: D1Database
      KV: KVNamespace
    }

    const app = createApp<Env>()

    // Health-check endpoint
    app.get('/', (req, res) => {
      res.json({ status: 'ok', region: req.cf?.colo ?? 'unknown' })
    })

    // Typed D1 query — req.env.DB is fully type-safe
    app.get('/users/:id', async (req, res) => {
      const user = await req.env.DB
        .prepare('SELECT id, name, email FROM users WHERE id = ?')
        .bind(req.params.id)
        .first()

      if (!user) return res.status(404).json({ error: 'User not found' })

      res.json(user)
    })

    // Export the fetch handler for Cloudflare Workers
    export default {
      fetch: app.fetch,
    }
    ```
  </Step>

  <Step title="Add wrangler.toml bindings">
    Open `wrangler.toml` and declare your D1 database binding. Replace `your-database-id` with the ID from `npx wrangler d1 create myapp`.

    ```toml wrangler.toml theme={null}
    name = "my-worker"
    main = "src/index.ts"
    compatibility_date = "2024-09-23"

    [[d1_databases]]
    binding      = "DB"
    database_name = "myapp"
    database_id  = "your-database-id"
    ```

    The `binding` value here must match the key you used in your `Env` type (`DB`).
  </Step>

  <Step title="Run locally">
    Start the local development server with hot-reload. Wrangler emulates the full Workers runtime including D1, KV, and other bindings.

    ```bash theme={null}
    npx wrangler dev
    ```

    Visit `http://localhost:8787` to see your health-check response, and `http://localhost:8787/users/1` to test the D1 route.
  </Step>

  <Step title="Deploy">
    When you're ready to go live, deploy to Cloudflare's global network with a single command.

    ```bash theme={null}
    npx wrangler deploy
    ```

    Wrangler prints the deployed URL — your Worker is now live at the edge.
  </Step>
</Steps>

<Note>
  This quickstart covers the essentials to get you moving. For the full API — including sub-routers, middleware composition, error handling, and every `req`/`res` method — see the [Core Concepts](/core/app) documentation.
</Note>
