Documentation Index
Fetch the complete documentation index at: https://mintlify.com/alchemy-run/alchemy/llms.txt
Use this file to discover all available pages before exploring further.
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
Create a new project
Create a new directory and initialize your project:mkdir my-alchemy-app
cd my-alchemy-app
npm init -y
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
Create the Worker code
Create a file src/worker.ts with your Worker code: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;
}
Create the deployment script
Create alchemy.run.ts to define your infrastructure: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();
Deploy to Cloudflare
Run the deployment script:You should see output like:Worker deployed to: https://my-app-dev-worker.username.workers.dev
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:
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