← SQL to ER Diagram
Your token

SQL to ER Diagram API

The diagram, the free checks and every export run free in the browser and need no API. The API is for the one metered step: send a schema, get back a design review as JSON, with fix SQL for each finding.

Base URL: https://api.skillsafe.ai/v1/app-api. Every response is {"data": ...} or {"error": {"code", "message"}}; read error.code and error.message rather than the HTTP status alone.

The task field

This app has one task. Always send "task": "review".

taskWhat it doesReply
reviewReviews CREATE TABLE statements as a database engineer would and returns up to 10 new findings (missing unique keys, NOT NULLs, CHECKs, indexes for lookups, data stored twice, delete rules, junction tables and more), each with fix SQL in the given dialect, plus which of the page's own checks it agrees with or disputes.One ```json block

Input fields

FieldTypeMeaning
taskstring"review"
sqlstringThe schema: CREATE TABLE, ALTER TABLE and CREATE INDEX statements. The page sends at most 14,000 characters of whole statements, without comments or data.
focusstringOptional, up to 200 characters: how the database is used, e.g. "customers sign in by email".
dialectstringpostgres, mysql, sqlite, sqlserver or generic. Fix SQL is written for it.
factsstringJSON text: tables (name, pk, column count, foreign keys) and checks (the page's own findings, each {id, kind, table, columns}). Send "{}" if you have nothing; the reply's agreed and disputed are then empty.
clipped_notestringEmpty, or a sentence saying what was left out of sql.

The reply

job.output.output is text: one ```json block holding {"task", "findings", "agreed", "disputed"}. Each finding has kind (one of missing_primary_key, missing_foreign_key, missing_fk_index, missing_index, missing_not_null, missing_unique, missing_check, wrong_type, repeating_group, denormalized_copy, delete_rule, junction_table, polymorphic_reference, missing_timestamps, naming, other), severity (high, medium, low), table, columns, why (one sentence) and fix_sql (ALTER TABLE, CREATE INDEX or CREATE TABLE statements, or empty). If the balance ran low, truncated is true and the JSON may stop early; complete findings before the cut can still be read.

Worked example

Input (input.json):

{
  "task": "review",
  "sql": "CREATE TABLE customers (\n  id BIGSERIAL PRIMARY KEY,\n  email VARCHAR(255) NOT NULL UNIQUE,\n  full_name VARCHAR(120) NOT NULL,\n  created_at TIMESTAMPTZ NOT NULL DEFAULT now()\n);\n\nCREATE TABLE addresses (\n  id BIGSERIAL PRIMARY KEY,\n  customer_id BIGINT NOT NULL REFERENCES customers(id) ON DELETE CASCADE,\n  line1 VARCHAR(200) NOT NULL,\n  city VARCHAR(100) NOT NULL,\n  postcode VARCHAR(20)\n);\n\nCREATE TABLE products (\n  id BIGSERIAL PRIMARY KEY,\n  sku VARCHAR(40) NOT NULL UNIQUE,\n  name VARCHAR(200) NOT NULL,\n  price FLOAT NOT NULL,\n  category_id BIGINT\n);\n\nCREATE TABLE categories (\n  id BIGSERIAL PRIMARY KEY,\n  parent_id BIGINT REFERENCES categories(id),\n  name VARCHAR(100) NOT NULL\n);\n\nCREATE TABLE orders (\n  id BIGSERIAL PRIMARY KEY,\n  customer_id BIGINT NOT NULL REFERENCES customers(id),\n  shipping_address_id BIGINT REFERENCES addresses(id),\n  status VARCHAR(20) NOT NULL DEFAULT 'new',\n  placed_at TIMESTAMPTZ NOT NULL DEFAULT now()\n);\n\nCREATE TABLE order_items (\n  order_id BIGINT NOT NULL REFERENCES orders(id) ON DELETE CASCADE,\n  product_id BIGINT NOT NULL REFERENCES products(id),\n  quantity INT NOT NULL CHECK (quantity > 0),\n  unit_price NUMERIC(10,2) NOT NULL,\n  PRIMARY KEY (order_id, product_id)\n);\n\nCREATE INDEX idx_orders_customer ON orders (customer_id);",
  "focus": "Online shop: customers sign in by email, staff list orders by status every day.",
  "dialect": "postgres",
  "facts": "{\"dialect\":\"postgres\",\"tables\":[{\"name\":\"customers\",\"pk\":[\"id\"],\"columns\":4,\"fks\":[]},{\"name\":\"addresses\",\"pk\":[\"id\"],\"columns\":5,\"fks\":[\"customer_id->customers on delete cascade\"]},{\"name\":\"products\",\"pk\":[\"id\"],\"columns\":5,\"fks\":[]},{\"name\":\"categories\",\"pk\":[\"id\"],\"columns\":3,\"fks\":[\"parent_id->categories\"]},{\"name\":\"orders\",\"pk\":[\"id\"],\"columns\":5,\"fks\":[\"customer_id->customers\",\"shipping_address_id->addresses\"]},{\"name\":\"order_items\",\"pk\":[\"order_id\",\"product_id\"],\"columns\":4,\"fks\":[\"order_id->orders on delete cascade\",\"product_id->products\"]}],\"checks\":[{\"id\":\"c1\",\"kind\":\"missing_fk_index\",\"table\":\"addresses\",\"columns\":[\"customer_id\"]},{\"id\":\"c2\",\"kind\":\"missing_fk_index\",\"table\":\"categories\",\"columns\":[\"parent_id\"]},{\"id\":\"c3\",\"kind\":\"missing_fk_index\",\"table\":\"orders\",\"columns\":[\"shipping_address_id\"]},{\"id\":\"c4\",\"kind\":\"missing_fk_index\",\"table\":\"order_items\",\"columns\":[\"product_id\"]},{\"id\":\"c5\",\"kind\":\"float_for_money\",\"table\":\"products\",\"columns\":[\"price\"]},{\"id\":\"c6\",\"kind\":\"missing_foreign_key\",\"table\":\"products\",\"columns\":[\"category_id\"]},{\"id\":\"c7\",\"kind\":\"missing_on_delete\",\"table\":\"\",\"columns\":[]},{\"id\":\"c8\",\"kind\":\"missing_timestamps\",\"table\":\"\",\"columns\":[]}]}",
  "clipped_note": ""
}

Reply:

```json
{
  "task": "review",
  "findings": [
    {
      "kind": "missing_index",
      "severity": "medium",
      "table": "orders",
      "columns": ["status"],
      "why": "Staff list orders by status every day, but orders.status has no index to serve that filter.",
      "fix_sql": "CREATE INDEX idx_orders_status ON orders (status);"
    },
    {
      "kind": "missing_check",
      "severity": "medium",
      "table": "orders",
      "columns": ["status"],
      "why": "orders.status is free text, so typos like 'nwe' are accepted; a CHECK must list every allowed status, and only 'new' is known here.",
      "fix_sql": ""
    },
    {
      "kind": "missing_check",
      "severity": "low",
      "table": "products",
      "columns": ["price"],
      "why": "products.price accepts negative values, which no product price should have.",
      "fix_sql": "ALTER TABLE products ADD CONSTRAINT products_price_check CHECK (price >= 0);"
    }
  ],
  "agreed": ["c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8"],
  "disputed": []
}
```

Paste the schema into the page to draw it, run the free checks and add the fixes.

Errors

HTTPWhat to do
401The token is missing, expired or a guest token on a run. Get a personal token on the token page.
402The balance is below what the run reserves. Top up at skillsafe.ai/account/billing.
400The body is not a JSON object of the fields above.
429Wait and retry with the same Idempotency-Key.

Step 1 — a tiny client helper

One function that sends JSON and unwraps the {data, error} envelope.

BASE="https://api.skillsafe.ai/v1/app-api"
TOKEN="paste-your-token-here"   # from https://sql-to-erd.skillsafe.ai/tokens.html
api() { # api METHOD PATH [JSON-BODY]
  curl -s -X "$1" "$BASE$2" -H "Content-Type: application/json" \
    ${TOKEN:+-H "Authorization: Bearer $TOKEN"} ${3:+--data "$3"}
}

Step 2 — a token

A guest token is enough for /me and /estimate. A run is metered, so it needs your personal token: sign in on the token page and copy it.

# a guest token (enough for /me and /estimate); a run needs your personal token from /tokens.html
TOKEN=$(curl -s -X POST "$BASE/guest" -H "Content-Type: application/json" -d '{"slug":"sql-to-erd"}' | jq -r '.data.token')

Step 3 — who am I, and my balance

subject_type is guest or user; credits is the balance (1 credit = $0.0001).

api GET /me | jq '.data | {subject_type, credits}' 

Step 4 — price a run for free

/estimate charges nothing and starts no job. hold_credits is the amount reserved while a run is in flight, not the price; the run is charged what it uses.

# input.json: the run input object - {"task": "review", "sql": "...", "focus": "", "dialect": "postgres", "facts": "{...}", "clipped_note": ""}
api POST /estimate "$(cat input.json)" | jq '.data | {hold_credits, min_credits, model, model_alias, markup_bps, warnings}' 

Step 5 — run it and wait for the job

Send an Idempotency-Key derived from the input: a retry with the same key is never billed twice.

KEY="sql-to-erd:review:$(shasum -a 256 input.json | cut -c1-16):a1"
JOB=$(curl -s -X POST "$BASE/run" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: $KEY" --data @input.json | jq -r '.data.job_id')
while :; do
  J=$(api GET "/jobs/$JOB"); S=$(echo "$J" | jq -r '.data.status')
  case "$S" in succeeded|failed|cancelled) break;; esac; sleep 2
done
echo "$J" | jq -r '.data.output.output' > reply.txt   # one ```json block: {task, findings, agreed, disputed}
echo "$J" | jq '.data | {charged_credits, truncated}' 

Step 6 — or stream it

/run-stream sends Server-Sent Events while the job runs and the finished job at the end.

# Server-Sent Events: tick/delta events while the job runs, then a final event with the job
curl -N -s -X POST "$BASE/run-stream" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: sql-to-erd:review:$(shasum -a 256 input.json | cut -c1-16):a1" --data @input.json