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.
Creating Resources
Alchemy uses a pseudo-class pattern for defining infrastructure resources. Each resource is created using theResource() function, which manages the complete lifecycle (create, update, delete) of your infrastructure.
Basic Resource Creation
Resources are created by calling a resource constructor function with an ID and props:.alchemy/--destroy, --stage, etc.)"api", "database")const bucket = await R2Bucket("storage", {
name: "my-bucket"
});
const database = await D1Database("db", {
name: "my-database"
});
Resource IDs and Physical Names
Every resource has two important identifiers:Resource ID
The ID is the logical identifier used in your Alchemy code:- Must be unique within a scope
- Used to reference the resource in state
- Cannot contain colons (
:)
Physical Name
The physical name is the actual name in the cloud provider. By default, Alchemy generates this as:Resource Lifecycle
Alchemy automatically manages the complete lifecycle of your resources:Create Phase
When you run youralchemy.run.ts script, new resources are created:
Update Phase
If you change resource properties and re-run, Alchemy updates the resource:Delete Phase
Run with--destroy to delete all resources:
Alchemy tracks which resources exist in your code. If you remove a resource from your script, it will be automatically deleted on the next run (orphan cleanup).
Resource References
You can pass resources as properties to other resources:- Resolves resource dependencies
- Creates resources in the correct order
- Extracts the necessary properties for bindings
Concurrent Resource Creation
Resources can be created concurrently when they don’t depend on each other:Adopting Existing Resources
If a resource already exists with the same name, you can adopt it:Resource Outputs
Every resource returns output properties you can use:- Display deployment information
- Configure other resources
- Pass to external systems
Error Handling
Alchemy provides clear error messages for common issues:- Duplicate resource ID: Using the same ID twice in a scope
- Invalid resource ID: IDs containing colons or invalid characters
- Resource conflicts: Resource already exists without
adopt: true - Missing credentials: Provider credentials not configured
Best Practices
// Good
const apiWorker = await Worker("api", { /* ... */ });
const userDb = await D1Database("user-db", { /* ... */ });
// Avoid
const w1 = await Worker("w1", { /* ... */ });
const db = await D1Database("db", { /* ... */ });
// API Infrastructure
const apiDb = await D1Database("api-db", { /* ... */ });
const apiWorker = await Worker("api", {
bindings: { DB: apiDb }
});
// Frontend Infrastructure
const assets = await R2Bucket("assets", { /* ... */ });
const frontend = await Vite("frontend", {
bindings: { ASSETS: assets }
});
Next Steps
- Managing Secrets - Securely store API keys and credentials
- Local Development - Test resources locally before deployment
- Testing - Write tests for your infrastructure