VS Code Extensions for Cloudflare D1¶
Guide to managing Cloudflare D1 databases directly from Visual Studio Code.
Two Options Available¶
- Cloudflare D1 Extension (Simpler, Recommended)
- DBCode (More features, professional)
Option 1: Cloudflare D1 Extension (Recommended)¶
Best for: Simple database browsing and queries
Installation¶
Method 1 - Quick Install:
1. Press Ctrl+P (Windows/Linux) or Cmd+P (Mac)
2. Paste: ext install yawarjamal.cf-d1
3. Press Enter
Method 2 - Extensions Marketplace:
1. Open VS Code
2. Click Extensions icon (or press Ctrl+Shift+X)
3. Search for "Cloudflare D1"
4. Click Install on "Cloudflare D1" by yawarjamal
Setup Steps¶
1. Get Your Cloudflare Credentials¶
You need: - Cloudflare Account Email - Cloudflare API Key (NOT API Token)
Get API Key: 1. Go to: https://dash.cloudflare.com/profile/api-tokens 2. Scroll to "API Keys" section (not "API Tokens") 3. Click "View" next to "Global API Key" 4. Enter your password 5. Copy the API Key
2. Connect to Cloudflare¶
- Open VS Code
- Click the D1 logo in the left activity bar (sidebar)
- Click "Add Account"
- Enter:
- Email: Your Cloudflare account email
- API Key: The Global API Key from step 1
- Click Save
3. Select Your Database¶
- In the D1 sidebar, your account appears
- Expand your account
- You'll see all your D1 databases
- Click on test-db to connect
4. Browse and Query¶
View Tables: - Expand your database in the sidebar - See all tables: users, rooms, user_rooms, room_types - Click a table to see its structure
Run Queries:
1. Create a new file with .sql extension (e.g., query.sql)
2. Write your SQL:
SELECT * FROM users;
Available Commands¶
Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) to open Command Palette:
D1: Add Account- Add new Cloudflare accountD1: Remove Account- Remove account credentialsD1: Switch Database- Change active databaseD1: Execute Query- Run current SQL file
Example Queries¶
Create a file called queries.sql:
-- List all users
SELECT * FROM users;
-- Count users
SELECT COUNT(*) as total_users FROM users;
-- Get rooms with users
SELECT r.name, COUNT(ur.user_id) as user_count
FROM rooms r
LEFT JOIN user_rooms ur ON r.id = ur.room_id
GROUP BY r.id, r.name;
-- Insert a test user
INSERT INTO users (external_id, created_at, updated_at)
VALUES ('test-user-1', datetime('now'), datetime('now'));
-- View room types
SELECT * FROM room_types;
Select the query you want to run and click "Run Query".
Features¶
✅ Multi-account support ✅ Database switching ✅ Table browsing ✅ Query execution with results ✅ Schema exploration ✅ Integrated SQL editor
Limitations¶
❌ No visual query builder ❌ No CSV/JSON export (yet) ❌ No autocomplete (yet)
Option 2: DBCode (Professional Tool)¶
Best for: Advanced database management, visual tools
Installation¶
- Open VS Code Extensions
- Search for "DBCode"
- Install DBCode by DBCode
- Restart VS Code
Setup Steps¶
1. Create Cloudflare API Token¶
Important: DBCode uses API Tokens (not API Keys like the first extension)
- Go to: https://dash.cloudflare.com/profile/api-tokens
- Click "Create Token"
- Click "Create Custom Token"
- Configure:
- Token name: "DBCode Access"
- Permissions:
- Account → D1 → Edit
- Account → Account Settings → Read
- Click "Continue to summary"
- Click "Create Token"
- Copy the token (you won't see it again!)
2. Get Your Account ID¶
Method 1:
wrangler whoami
Method 2:
- Go to any Cloudflare dashboard page
- Look at the URL: https://dash.cloudflare.com/{ACCOUNT_ID}/...
- Copy the ACCOUNT_ID
3. Connect DBCode to Cloudflare¶
For a Single Database: 1. Open DBCode extension in VS Code (database icon in sidebar) 2. Click "Add Connection" button (+ icon) 3. Select "Cloudflare D1" from the list 4. Enter: - Connection Name: "Production D1" - Account ID: Your Cloudflare Account ID - Database ID: Your Database ID (from Cloudflare dashboard) - API Token: Your API Token from step 1 5. Click "Save"
For Multiple Databases (Cloud Provider): 1. Open DBCode extension 2. Click "Add Connection" 3. Choose "Cloudflare" cloud provider (from right panel) 4. Enter: - Account ID: Your account ID - API Token: Your API token 5. Click "Connect" 6. DBCode will list all databases in your account 7. Click any database to connect
4. Using DBCode¶
Browse Tables: - Expand your connection in DBCode sidebar - See all tables and their columns - Right-click table for options
Run Queries:
1. Click "New Query" button
2. Write SQL
3. Click "Execute" or press F5
4. View results in table/JSON format
Visual Tools: - Data Viewer: Browse table data with pagination - Table Designer: View/edit table structure - Query Builder: Visual query creation - Export Data: Export to CSV, JSON, SQL
DBCode Features¶
✅ Visual query builder ✅ Advanced data viewer ✅ Export to CSV/JSON/SQL ✅ Multi-database connections ✅ Schema designer ✅ Code autocomplete ✅ Query history ✅ Dark/light themes
DBCode vs Simple Extension¶
| Feature | Cloudflare D1 Extension | DBCode |
|---|---|---|
| Setup Complexity | Simple (email + API key) | Medium (API token required) |
| Price | Free | Free |
| Query Execution | ✅ | ✅ |
| Table Browsing | ✅ | ✅ |
| Visual Query Builder | ❌ | ✅ |
| Data Export | ❌ | ✅ |
| Autocomplete | ❌ | ✅ |
| Multi-database | ✅ | ✅ |
Quick Start Guide¶
For Your Project¶
Based on your current setup:
Credentials you have: - Account ID: {{CLOUDFLARE_ACCOUNT_ID}} - Database ID: {{CLOUDFLARE_DB_ID}} - Database Name: {{CLOUDFLARE_DB_NAME}}
Choose your extension:
Simple Option (Cloudflare D1):
# Install
ext install yawarjamal.cf-d1
# Then in VS Code:
1. Click D1 icon in sidebar
2. Add account with your Cloudflare email + Global API Key
3. Select test-db database
4. Start querying!
Professional Option (DBCode):
# Install DBCode extension
# Then in VS Code:
1. Click DBCode icon
2. Add Connection → Cloudflare D1
3. Enter:
- Account ID: {{**CLOUDFLARE_ACCOUNT_ID**}}
- Database ID: {{**CLOUDFLARE_DB_ID**}}
- API Token: [Create new token with D1 Edit permissions]
4. Connect and start working!
Common Issues & Solutions¶
Issue: "Authentication Failed"¶
For Cloudflare D1 Extension: - Verify you're using Global API Key, not API Token - Check email is correct - Try regenerating API Key
For DBCode: - Verify API Token has correct permissions: - Account → D1 → Edit - Account → Account Settings → Read - Check Account ID is correct - Token might be expired - create new one
Issue: "Database Not Found"¶
Solution: - Verify Database ID is correct:
wrangler d1 list
Issue: "No Tables Showing"¶
Solution:
# Verify tables exist remotely
wrangler d1 execute test-db --remote --command "SELECT name FROM sqlite_master WHERE type='table'"
If empty, run migrations:
wrangler d1 execute test-db --remote --file=./001_migration.sql
Issue: "Query Fails with Permission Error"¶
Solution: - API Token needs D1 → Edit permission (not just Read) - Recreate token with proper permissions - Update token in VS Code extension
Example Workflows¶
Workflow 1: Inspect Database Schema¶
- Open Cloudflare D1 extension
- Expand test-db → Tables
- Click users table
- See columns: id, external_id, created_at, updated_at
- Repeat for other tables
Workflow 2: Query and Export Data¶
Using DBCode: 1. Create new query 2. Write:
SELECT u.external_id, r.name as room_name
FROM users u
JOIN user_rooms ur ON u.id = ur.user_id
JOIN rooms r ON ur.room_id = r.id;
Workflow 3: Test Migrations¶
- Create
test-query.sql:-- Check if migration applied SELECT COUNT(*) FROM users; SELECT COUNT(*) FROM rooms; -- Verify indexes exist SELECT name FROM sqlite_master WHERE type='index'; - Run query
- Verify results match expected schema
Tips & Best Practices¶
Security¶
-
Never commit API keys/tokens to git
# Add to .gitignore .vscode/settings.json # If it contains credentials -
Use scoped tokens (DBCode)
- Create separate tokens for development and production
- Limit permissions to minimum required
-
Rotate credentials regularly
- Change tokens every 90 days
- Revoke old tokens
Performance¶
-
Use LIMIT for large queries
SELECT * FROM users LIMIT 100; -
Check query execution time
- DBCode shows execution time
- Optimize slow queries
-
Create indexes for frequent queries
CREATE INDEX idx_users_external_id ON users(external_id);
Tips¶
-
Save common queries
- Create a
queries/folder - Save frequently used queries as
.sqlfiles
- Create a
-
Use SQL snippets
- Create VS Code snippets for common patterns
- Example: INSERT, SELECT with JOINs