Skip to main content
Get up and running with Alchemy by deploying a Cloudflare Worker with storage and a database.

Prerequisites

  • Node.js 18+ or Bun
  • A Cloudflare account with an API token (create one here)

Install Alchemy

1

Create a new project

Create a new directory and initialize your project:
mkdir my-alchemy-app
cd my-alchemy-app
npm init -y
2

Install Alchemy

npm install alchemy
3

Set up credentials

Create a .env file with your Cloudflare credentials:
CLOUDFLARE_API_TOKEN=your-api-token
CLOUDFLARE_ACCOUNT_ID=your-account-id
Find your Account ID in the Cloudflare dashboard URL: dash.cloudflare.com/<account-id>

Create your first deployment

1

Create the Worker code

Create a file src/worker.ts with your Worker code:
src/worker.ts
export default {
  async fetch(request: Request, env: Env) {
    const url = new URL(request.url);
    
    // Get visitor count from KV
    const count = parseInt((await env.VISITS.get("count")) || "0");
    const newCount = count + 1;
    await env.VISITS.put("count", newCount.toString());
    
    // Store visit info in R2
    await env.STORAGE.put(
      `visit-${Date.now()}.json`,
      JSON.stringify({
        timestamp: Date.now(),
        url: url.pathname,
        count: newCount,
      })
    );
    
    return Response.json({
      message: "Hello from Alchemy!",
      visits: newCount,
    });
  },
};

interface Env {
  VISITS: KVNamespace;
  STORAGE: R2Bucket;
}
2

Create the deployment script

Create alchemy.run.ts to define your infrastructure:
alchemy.run.ts
import alchemy from "alchemy";
import { Worker, KVNamespace, R2Bucket } from "alchemy/cloudflare";

const app = await alchemy("my-app");

// Create KV namespace for visit counting
const visits = await KVNamespace("visits", {
  name: `${app.name}-${app.stage}-visits`,
});

// Create R2 bucket for storing visit data
const storage = await R2Bucket("storage", {
  name: `${app.name}-${app.stage}-storage`,
});

// Deploy the Worker
const worker = await Worker("worker", {
  name: `${app.name}-${app.stage}-worker`,
  entrypoint: "./src/worker.ts",
  bindings: {
    VISITS: visits,
    STORAGE: storage,
  },
  url: true, // Enable workers.dev subdomain
});

console.log(`Worker deployed to: ${worker.url}`);

await app.finalize();
3

Deploy to Cloudflare

Run the deployment script:
npx tsx alchemy.run.ts
You should see output like:
Worker deployed to: https://my-app-dev-worker.username.workers.dev
4

Test your Worker

Visit the URL in your browser or use curl:
curl https://my-app-dev-worker.username.workers.dev
Response:
{
  "message": "Hello from Alchemy!",
  "visits": 1
}

Make updates

Update your Worker code in src/worker.ts, then re-run the deployment script. Alchemy will detect the changes and update only what’s necessary:
npx tsx alchemy.run.ts

Tear down

To delete all resources:
npx tsx alchemy.run.ts --destroy

What’s next?

Core concepts

Learn about resources, scopes, and lifecycle

Local development

Develop and test locally with Miniflare

Manage secrets

Securely handle API keys and credentials

More examples

Browse complete examples

Build docs developers (and LLMs) love