Skip to content

API Backends

Build custom APIs with Hono, test them in the API console, and store data in D1 or your own Postgres database

An API Backend is an app in your project that serves your own API. It’s built with Hono and TypeScript, and the AI agent writes the routes, database schema, and tests for you. Instead of a visual preview, the builder opens an API console where you can see every endpoint, send test requests, and read the logs.

Your mobile apps, web apps, and websites in the same project can share one API Backend.

API Backends live inside a project. To add one to a project that doesn’t have a backend yet:

  1. Open your project and click Add app.
  2. Choose Backend, then Your own API backend.
  3. Choose a database (see below), how people sign in, and any features the backend needs.
  4. Click Start building.

You need the Editor role or higher to add apps to a project. Apps you add to the project later use its backend.

OptionWhat you get
Cloudflare D1A managed SQL database at the edge, created for you. This is the default. See D1 database.
Supabase PostgresA dedicated, managed Postgres database.
Your PostgresConnect a Postgres database you already have.
Not sure yetNo database. You can add one later.

For Your Postgres, enter a Development connection URL (it starts with postgres:// or postgresql://). You can also add a Migration URL (optional) if migrations should use a different connection. Credentials are encrypted when they’re saved. You connect the staging and production databases later, from the Database tab. See External Postgres.

To add a database to an API Backend created with Not sure yet, open Integrations and go to the Database tab. Choose Provision Cloudflare D1, Provision Supabase, or Connect Postgres. Once a database has data in it, you can’t switch it to a different provider automatically.

The agent follows a consistent structure, so your API stays easy to test and document:

  • Hono. Your API runs in a serverless runtime, which has no filesystem and doesn’t support long-running processes.
  • OpenAPI routes. Each route declares its request and response schemas, and the API serves an OpenAPI document at /doc. The API console and API Docs are both built from this document, so a route only shows up there if it’s declared this way.
  • Versioned paths. Public routes live under /v1. /doc and the /healthz health check are always public.
  • API keys by default. Every other route requires an API key, including new ones the agent adds.
  • Database migrations in your repo. Schema changes are written as migrations and saved with your code, so every environment gets the same schema when you publish.
  • Tests. The agent can write and run tests in the same runtime your published API uses.

Store secrets such as third-party API keys as environment variables in Settings, not in your code. For an API Backend, every variable is stored as a secret and is available only to your API at runtime. When you add a variable, choose which environments it applies to, so development, staging, and production can each use their own value.

Draftbit sets your database’s connection details for each environment itself. With External Postgres, the connection URL is in DATABASE_URL and the migration URL is in DATABASE_MIGRATION_URL. While the app has a database, those names are reserved, so you can’t add your own variables with them.

The API console is the main view of an API Backend. It has three parts: Endpoints on the left, the request playground in the center with Request Logs below it, and the agent on the right. On a phone, use the tabs along the bottom (Endpoints, Console, Agent) to switch between them.

The Endpoints list updates on its own as the agent adds or changes routes. Endpoints are grouped by tag, or by the first part of their path, and each shows its method, path, and summary. Use Search endpoints… to filter by path, method, summary, or tag.

If the list is empty, check the message it shows. The development server may still be starting, or the API may have no routes yet. If the OpenAPI document is missing or invalid, ask the agent to fix it.

Select an endpoint to open it in the playground. Fill in any path parameters in the request bar, then click Send. Requests always go to your development server.

TabUse it to
ParamsAdd query parameters.
HeadersAdd request headers. Content-Type is filled in for requests with a body.
BodyEdit the JSON body. It starts with the example from the endpoint’s schema, and Reset to example restores it.
AuthSee the development API key the playground attaches for you. Turn on Send without API key to check that the endpoint rejects unauthorized requests.

The response shows the status, how long the request took, and the response size, with Body and Headers tabs. Click Copy as cURL to copy the request as a command you can run elsewhere. The copied command has a placeholder where your API key goes.

The playground remembers each endpoint’s request and last response while you switch between endpoints. Viewers can browse endpoints but need the Editor role to send requests.

Open Request Logs from the bar at the bottom of the playground.

  • Requests lists recent requests, newest first, with the method, path, status, duration, and time. Expand a request to see the logs it produced. Your latest request from the playground is highlighted.
  • Raw shows the full development server log.

API Docs in the builder navigation shows documentation generated from your API’s OpenAPI document. For each endpoint you get its parameters, request body, responses, and whether it requires an API key. Wide screens also show a cURL example you can copy. Everyone with access to the app can read API Docs.

Your API can run jobs on a schedule, such as sending a daily summary email or deleting expired sessions. Ask the agent for the job and how often it should run. The agent writes the job and adds its schedule to triggers.crons in wrangler.jsonc as a cron expression, such as 0 3 * * * for every day at 3:00 AM UTC. To change or remove a schedule, ask the agent again.

A schedule starts running in Staging or Production the next time you publish to that environment, and stops after you remove it and publish again. Schedules don’t run in Development, but the agent can run a job there to test it.

Scheduled Jobs in the builder navigation shows:

  • In your code: every schedule in your current code, when it runs next, and whether it’s published yet.
  • Staging and Production: the schedules running in each published environment, when each runs next, and how its last run went, including the error if it failed.

Run now runs a published job right away, on top of its schedule, and its result shows up a few seconds later. In Production you confirm first. You need the Editor role or higher to use Run now.

  • Schedules use UTC and run on 5-minute marks, so the minute in each schedule must be a multiple of 5, such as 0, 15, or */5. The most often a job can run is every 5 minutes.
  • For the day of the week, use names such as MON-FRI. Numbers follow Cloudflare’s cron format, where 1 is Sunday and 7 is Saturday.
  • Each environment runs up to 5 schedules. Schedules that break a limit are flagged under In your code and aren’t published.
  • A job has the same CPU time limit as a request to your API, so split long work into smaller runs.
  • A job never overlaps itself: if a run is still going when the next one is due, that run is skipped.
  • A run that fails or is missed isn’t retried. The next one runs on schedule.

D1 is a managed SQL database from Cloudflare. When your API Backend uses D1, Draftbit creates the database for you. Your API reaches it directly, so there’s no connection URL to manage.

Each environment has its own database:

  • Development is created with the app and is the one the agent and the API console use.
  • Staging and Production are created the first time you publish to that environment.

While you build, your Development data lives in your sandbox. Draftbit backs up the Development database as you work and restores it if your sandbox is rebuilt, so the accounts and records you create while testing are still there next time. Two things aren’t covered yet: files your API stores, and branches other than your default branch.

Resetting the Development database is permanent. The backup follows the reset, so the earlier data can’t be brought back.

If your API doesn’t start and Logs says Draftbit could not restore this app's Development database from its backup, select Restart preview in the main menu, or ask the agent to restart it. Draftbit holds the preview back in that case, so it never opens on an empty database while your data is still in the backup.

There are two ways to open the D1 workspace:

  • On your project page, click Manage D1 on the API Backend’s card.
  • In the builder, open Integrations and go to the Database tab.

You need the Editor role or higher. Pick an environment, then use the sections:

SectionWhat it shows
OverviewThe database’s storage, table count, and settings.
TablesYour tables. Select one to browse its first 100 rows.
SQLRun SQL queries. Results show up to 500 rows.
BackupsRestore the database to any point in the last 30 days, or download a copy as a .sql file.
MetricsReads, writes, rows read and written, and the queries using the most database time, for today, the last 7 days, or the last 30 days.

Table browsing is read-only. In SQL, a query that can change data or schema needs a second click to confirm. Before you can run SQL against Staging or Production, Draftbit asks you to confirm, and for production you type PRODUCTION.

Open Backups to undo changes to a database or keep a copy of it.

  • Restore to a point in time: pick a date and time, to the minute, from the last 30 days, click Restore followed by the environment name (for example, Restore Production), and confirm. Every table goes back to how it was at the start of that minute, and changes made since then are replaced. Queries running during the restore fail. For Production, you type PRODUCTION to confirm. If you change your mind, click Undo this restore right after it finishes to put back exactly what the restore replaced.
  • Download a copy: click Export .sql, then click the download link when it appears. The link works for one hour. While the export runs, your API can’t read or write the database, so for Staging and Production you confirm first, and it’s best to export production at a quiet time. Stay on Backups until the link appears: leaving the page stops the export, and an export that takes longer than 15 minutes is stopped too.

With External Postgres, your API Backend uses a Postgres database you host yourself, with any provider that gives you a postgres:// or postgresql:// connection URL. Draftbit doesn’t create or manage the database. It stores the connection details, gives them to your API, and runs your migrations against the database when you publish.

Each environment connects to its own database, and you add them one at a time: development first, then staging and production. Use a separate database for each environment, so testing in development or staging can’t change production data.

You connect the development database in one of two ways:

  • When you create the API Backend, choose Your Postgres as its database and fill in Development connection URL.
  • For an API Backend created with Not sure yet, open Integrations, go to the Database tab, and click Connect Postgres. Paste the connection URL, add an optional migration URL, and click Connect database.

Once it’s connected, the agent and the API console use this database.

  1. Open Integrations and go to the Database tab. You need the Editor role or higher.
  2. Click Configure staging.
  3. Under Staging Postgres, paste the staging database’s connection URL. If migrations should use a different connection, add it in the Optional migration URL field.
  4. Click Save connection.
  5. Click Configure production and repeat the steps for production.

Configure production appears once staging is saved. Each environment you’ve connected is listed on the Database tab with Credentials configured and a status dot, which turns green when the connection is active.

To fix a wrong URL or rotate credentials, click Replace URL next to the environment on the Database tab, paste the new connection URL (and migration URL, if you use one), and click Save connection. Saved URLs are never shown again, so you paste both in full; leaving the migration URL blank makes it use the new connection URL. Development switches right away. Staging and production use the new URL the next time you publish to them.

FieldWhat it’s for
Connection URLWhat your API uses to read and write data while it runs.
Migration URLWhat publishing uses to apply your migrations. If you leave it blank, it uses the connection URL.

Add a migration URL when your provider gives you separate connection strings, for example a pooled one for your app and a direct one for schema changes.

When you save a connection, Draftbit checks that the URL is a Postgres URL with a hostname. It doesn’t connect to the database at that point, so a wrong password or a blocked network shows up later, when your API, the console, or a publish tries to connect.

When an API Backend uses External Postgres, the Database tab also has a console for the development database. You need the Editor role or higher.

TabWhat it does
TablesLists your tables and how many columns each has. Select one to browse its first 100 rows.
SQLRun SQL against the development database and see the results.

Click Refresh schema after the agent adds or changes tables, so the list catches up. If the database has no tables yet, ask the agent to define a schema and run the migrations.

In SQL, a query that could change data or schema needs a second click: the button changes to Run destructive query?, and you click it again to run it. Results show up to 500 rows, and a query that runs longer than 15 seconds is stopped.

The console connects through your API’s own Postgres driver. If it says the postgres driver isn’t installed, ask the agent to set up Drizzle with postgres.js for your database.

  • The console only works with the development database. To look at staging or production data, use your database provider’s tools.
  • You can’t switch an API Backend from External Postgres to D1 or Supabase, or the other way around, automatically.

Your API can store files that people upload, such as profile photos or documents. Choose File storage when you add the API Backend, or ask the agent to add file uploads later.

With Cloudflare R2, Draftbit sets up the storage for you, and each environment keeps its own files:

  • Development stores files in your sandbox, so you can test uploads right away.
  • Staging and Production each get their own private bucket when you publish to that environment.

The bucket is private. People reach files only through your API, which decides who can read each one. By default, a file can be read only by the signed-in person who uploaded it, unless it was uploaded as public. Your API accepts images, PDFs, and plain text files up to 10 MB each. Ask the agent to change what it accepts.

Storage is connected when you publish. If uploads work in Development but fail in Staging or Production, publish that environment again.

To keep files in your own Amazon S3 bucket or another S3-compatible service instead, choose Amazon S3 and add these environment variables: S3_BUCKET, S3_REGION, S3_ACCESS_KEY_ID, and S3_SECRET_ACCESS_KEY. For a service other than Amazon S3, also set S3_ENDPOINT.

  • File storage needs an API Backend with a database.
  • Each environment has one bucket. If your code asks for more than one, publishing stops with an error, and the agent can combine them.
  • Files aren’t copied between environments, so a file uploaded in Development isn’t in Staging or Production.
  • Stored files aren’t part of your code, so ZIP exports, GitHub exports, and app backups don’t include them.
  • Deleting the API Backend deletes the files Draftbit stores for it. Files in your own S3 bucket stay there until you delete them with your provider.

Requests to your API need an active key in the x-api-key header. An Authorization: Bearer header with the key also works. Keys are scoped to one environment.

Draftbit creates a Development key for you, and publishing to staging or production creates a key for that environment if it doesn’t have one. That means a published API is protected from the start.

Manage keys in Publishing, on the API tab:

  • Create Key makes a new key for the environment you choose. Copy it when it appears. You can reveal it again later from the key list.
  • Copy key copies a key.
  • Rotate key revokes a key and issues a replacement.
  • Revoke key turns a key off.

Changes to the development key apply right away. Changes to staging and production keys apply the next time you publish to that environment. If an environment has no active keys, its API accepts all requests.

Your API runs in three environments:

EnvironmentWhere it runs
DevelopmentYour sandbox. It restarts as you build, and it’s what the API console calls.
StagingA deployed version with its own stable URL.
ProductionA deployed version with its own stable URL.

To deploy, click Publish in the top bar, then Publish in the API row. Name the version, choose Staging or Production, and start the publish. Publishing runs your database migrations against that environment’s database first. If your API Backend uses a Supabase database, publishing to Staging or Production needs a paid plan. A database that existed before paid backends launched can be published to on any plan until December 1, 2026.

After a publish, the API tab shows the environment’s URL, which you can copy, and its version. See Publishing for more about versions.

The Logs tab on the Publishing page shows your published API’s console output, errors, and requests for staging or production, from the last 7 days. Request Logs in the API console only cover development. See Logs.

You can serve your staging or production API from a domain you own, such as api.myapp.com. Custom domains are part of the paid plans.

  1. On the API tab, click Add custom domain on the Production or Staging card.
  2. Enter your domain and click Save.
  3. Add the CNAME and TXT records shown to your domain’s DNS. The CNAME points your domain to Draftbit, and the TXT lets Draftbit issue its SSL certificate.
  4. Once DNS and SSL show Verified, click Verify DNS records. Your domain starts serving the environment’s latest publish after the first successful check.

DNS changes can take a few minutes or several hours. Domains left unverified for 7 days are removed. Your API’s draftbit.dev URL keeps working alongside the custom domain. Unlike web apps, API domains don’t get a www redirect, so add the exact hostname your clients call.

The card then shows your domain. To disconnect it, click Manage on that card, then Remove domain. The same plan change rules as web custom domains apply.

Publishing checks that the environment’s database is ready before it deploys anything. If it isn’t, the publish stops with one of these messages (shown here for staging):

MessageWhat to do
staging external Postgres target is not configured.You haven’t connected a staging database yet. Click Configure staging on the Database tab, save the connection, and publish again. See External Postgres.
staging D1 target is not ready.Draftbit couldn’t create or reach the D1 database for that environment. On the Database tab, pick the environment. If it shows Retry provisioning, click it, then publish again.

If your migrations fail against an external database, check the migration URL (or the connection URL, if you didn’t set one) and that your database allows connections from the internet.

Apps in the same project can use your API Backend:

  • When you add a mobile app, web app, or website to a project that already has an API Backend, Add app asks which backend to use. If there’s only one, it’s picked for you.
  • When you create a project with Cloudflare D1 or Your Postgres, the new app is connected automatically.

Connecting adds two development environment variables to the app, one for the API’s URL and one for its development key. Their names start with the app’s prefix, for example VITE_API_URL and VITE_API_KEY for a web app, EXPO_PUBLIC_ for a mobile app, or PUBLIC_ for a website. You’ll find them in Settings. Set the staging and production values yourself when you’re ready to publish.

The agent in each app knows about the others. The backend’s agent knows which apps will call it, and your app’s agent can read the backend’s API.

While you build, the agent can also work with the other apps’ previews. It can check whether another app is running, read its preview logs, and use its development URL and development API key to connect the two. If that app isn’t running, the agent can start it, within your plan’s limits. When the agent changes another app’s code, it refreshes that app’s preview so the change shows up.