# 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.

- Endpoint: `POST https://apibox.io.vn/v1/dbml`
- Category: Developer utilities
- HTML docs: https://apibox.io.vn/docs/dbml/ (Vietnamese: https://apibox.io.vn/vi/docs/dbml/)
- Online tool: https://apibox.io.vn/tools/dbml/
- Authentication: `Authorization: Bearer <API key>` or `X-API-Key: <API key>` (free key: https://apibox.io.vn/dashboard/#signup)

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.

## Parameters

| Name | In | Type | Required | Default | Description |
|---|---|---|---|---|---|
| `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 request

```bash
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"}'
```

## Example response

```json
{
  "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.
