DBML ↔ SQL & ERD Diagram API
Convert DBML (dbdiagram.io syntax) to PostgreSQL, MySQL, SQL Server or SQLite DDL, SQL DDL back to DBML, and draw an ERD as SVG or PNG.
DBML is the readable schema language of dbdiagram.io. Send dbml and get CREATE statements for to=postgres (default), mysql, mssql or sqlite: tables with schemas, column types, primary keys (also composite), increment (IDENTITY / AUTO_INCREMENT / AUTOINCREMENT), unique, not null, defaults (strings, numbers, booleans, expressions), enums (CREATE TYPE in PostgreSQL, ENUM in MySQL, CHECK constraints elsewhere), indexes (composite, expression, unique, named, type: hash), notes as COMMENT ON / COMMENT / extended properties, and foreign keys from inline or block refs (>, <, -, composite, delete: cascade). Many-to-many refs (<>) create a junction table.
The reverse works too: send sql (CREATE TABLE, CREATE INDEX, CREATE TYPE … AS ENUM, ALTER TABLE ADD FOREIGN KEY / PRIMARY KEY, COMMENT ON, MySQL inline KEY / ENUM / COMMENT, SQL Server [brackets] and IDENTITY) and get DBML, the JSON model (to=json) or the DDL of another database, e.g. MySQL dump to PostgreSQL.
to=svg or to=png draws an entity-relationship diagram: tables placed in layers (referenced tables to the left of the tables that point to them), primary keys highlighted, foreign keys marked, crow's-foot ends for "many", header colours from headercolor, theme=dark. The parser is APIBox's own (no @dbml/core); TablePartial, records and DBML 3 sticky notes are not supported, and expression indexes are written as given for every database.
Markdown · OpenAPI · Postman · llms.txt
Parameters
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
dbml | body | string | no | DBML source (or send sql). | |
sql | body | string | no | SQL DDL (CREATE TABLE …) to convert. | |
to | body | string | no | postgres (default for DBML), mysql, mssql, sqlite, dbml (default for SQL), json, svg or png. | |
theme | body | string | no | light | ERD: light or dark. |
show_types | body | boolean | no | true | ERD: false hides column types. |
scale | body | number | no | 1 | PNG scale (1–3). |
Example
curl -X POST "https://apibox.io.vn/v1/dbml" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"dbml":"Table users {\n id int [pk, increment]\n email varchar(255) [unique, not null]\n}\n\nTable posts {\n id int [pk]\n user_id int [ref: > users.id]\n title varchar\n}","to":"postgres"}'const res = await fetch('https://apibox.io.vn/v1/dbml', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"dbml": "Table users {\n id int [pk, increment]\n email varchar(255) [unique, not null]\n}\n\nTable posts {\n id int [pk]\n user_id int [ref: > users.id]\n title varchar\n}",
"to": "postgres"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/dbml",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"dbml": "Table users {\n id int [pk, increment]\n email varchar(255) [unique, not null]\n}\n\nTable posts {\n id int [pk]\n user_id int [ref: > users.id]\n title varchar\n}", "to": "postgres"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/dbml');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"dbml":"Table users {\\n id int [pk, increment]\\n email varchar(255) [unique, not null]\\n}\\n\\nTable posts {\\n id int [pk]\\n user_id int [ref: > users.id]\\n title varchar\\n}","to":"postgres"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Response
{
"from": "dbml",
"to": "postgres",
"tables": 2,
"columns": 5,
"refs": 1,
"enums": 0,
"indexes": 0,
"sql": "CREATE TABLE \"users\" (\n \"id\" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,\n \"email\" varchar(255) UNIQUE NOT NULL\n);\n\nCREATE TABLE \"posts\" (\n \"id\" int PRIMARY KEY,\n \"user_id\" int,\n \"title\" varchar\n);\n\nALTER TABLE \"posts\" ADD FOREIGN KEY (\"user_id\") REFERENCES \"users\" (\"id\");\n"
}
Use cases
- Turn a dbdiagram.io design into a migration for PostgreSQL, MySQL or SQL Server
- Document an existing database: SQL dump → DBML and an ERD image
- Port a MySQL schema to PostgreSQL (types, enums, AUTO_INCREMENT, comments)
- Generate ERD images for design documents and student database projects
FAQ
Is it compatible with dbdiagram.io?
It follows the DBML syntax: Project, Table (alias, settings, indexes, notes), Ref (inline, short, long, composite, actions), Enum, TableGroup and Note. TablePartial and records are not supported yet.
How are relationships turned into foreign keys?
For a > b the foreign key goes on a (many side); for a < b on b; a - b puts it on a; a <> b creates a junction table a_b with two foreign keys.
Can I get a PNG?
Yes, to=png renders the diagram (scale up to 3). SVG stays sharp at any size and is better for documents.
Related APIs
UUID Generator
Generate up to 1,000 UUID v4, time-ordered UUID v7, ULID, Nano ID or random hex IDs per request. Secure randomness, optional uppercase.
⏱Cron Expression Parser
Validate a cron expression and get its next run times in any timezone, with a plain-English description. Supports 5, 6 and 7-field cron syntax.
🕒Time & Timezone Converter
Get the current time in any timezone, convert between timezones and Unix timestamps (seconds or ms), with UTC offset and weekday for up to 30 zones.