← SQL Formatter
Your token

SQL Formatter API

Formatting, the free check and every export run free in the browser and need no API. The API is for the one metered step: send a query, get back the query rewritten with best practices, as JSON, with what changed.

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": "rewrite".

taskWhat it doesReply
rewriteRewrites SQL to the best practices of the write-query skill in the given dialect: fixes the free check's findings where SQL can fix them, CTEs instead of nested subqueries, explicit joins, NOT EXISTS, range filters on bare columns, meaningful aliases. Applies an optional request. Never writes a WHERE you did not give.One ```json block

Input fields

FieldTypeMeaning
taskstring"rewrite"
sqlstringThe SQL, one or more statements. The page sends it formatted, whole statements, at most 12,000 characters.
dialectstringOne of sql, postgresql, mysql, mariadb, transactsql, plsql, sqlite, bigquery, snowflake, redshift, spark, duckdb, trino, db2, hive.
requeststringOptional, up to 300 characters: a change you want, e.g. "only the last 30 days".
factsstringJSON text: checks (the free check's findings, each {id, kind, line, key}), tables, ctes, statements. Send "{}" if you have none; the reply's kept is 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", "sql", "same_results", "result_change", "changes", "kept", "notes"}. sql is the whole rewritten SQL. same_results is false when the rows or columns returned would differ, and result_change then says how. Each change has kind (fix, request, correctness, structure, performance or readability), check (a check id, for fix) and what. kept lists checks left as they were, each {check, reason}. If the balance ran low, truncated is true and the JSON may stop early; the page can still read the SQL when it came through whole.

Worked example

Input (input.json):

{
  "task": "rewrite",
  "sql": "SELECT\n  a.id,\n  a.email,\n  count(b.id) AS orders,\n  sum(b.total) revenue\nFROM\n  customers a\n  LEFT JOIN orders b ON b.customer_id = a.id\nWHERE\n  b.status = 'paid'\n  AND date (b.created_at) >= '2026-01-01'\n  AND a.email ILIKE '%@gmail.com'\nGROUP BY\n  1,\n  2\nHAVING\n  count(b.id) > 3\nORDER BY\n  revenue DESC\nLIMIT\n  50;",
  "dialect": "postgresql",
  "request": "",
  "facts": "{\"checks\":[{\"id\":\"c1\",\"kind\":\"left_join_filtered\",\"line\":10,\"key\":\"orders\"},{\"id\":\"c2\",\"kind\":\"function_on_column\",\"line\":11,\"key\":\"date|created_at\"},{\"id\":\"c3\",\"kind\":\"short_alias\",\"line\":7,\"key\":\"a,b\"},{\"id\":\"c4\",\"kind\":\"leading_wildcard\",\"line\":12,\"key\":\"email\"},{\"id\":\"c5\",\"kind\":\"ordinal_reference\",\"line\":14,\"key\":\"group\"}],\"tables\":[\"customers\",\"orders\"],\"ctes\":[],\"statements\":1}",
  "clipped_note": ""
}

Reply:

```json
{
  "task": "rewrite",
  "sql": "SELECT\n  cust.id,\n  cust.email,\n  count(ord.id) AS orders,\n  sum(ord.total) AS revenue\nFROM customers AS cust\nINNER JOIN orders AS ord ON ord.customer_id = cust.id\nWHERE ord.status = 'paid'\n  -- bare column range so an index on created_at can be used\n  AND ord.created_at >= '2026-01-01'\n  AND cust.email ILIKE '%@gmail.com'\nGROUP BY cust.id, cust.email\nHAVING count(ord.id) > 3\nORDER BY revenue DESC\nLIMIT 50;",
  "same_results": true,
  "result_change": "",
  "changes": [
    {"kind": "fix", "check": "c1", "what": "Made the orders join INNER, since the paid-status filter on orders already dropped customers without matching orders."},
    {"kind": "fix", "check": "c2", "what": "Compared orders.created_at directly to '2026-01-01' instead of wrapping it in date()."},
    {"kind": "fix", "check": "c3", "what": "Renamed aliases a and b to cust for customers and ord for orders."},
    {"kind": "fix", "check": "c5", "what": "Grouped by cust.id and cust.email instead of the positions 1 and 2."},
    {"kind": "readability", "check": "", "what": "Added AS before the revenue alias and put each clause on its own line."}
  ],
  "kept": [
    {"check": "c4", "reason": "The ILIKE pattern '%@gmail.com' must match the end of customers.email, so the leading wildcard stays."}
  ],
  "notes": ["An index on orders (customer_id, status, created_at) would serve the join and the filters."]
}
```

Paste the SQL into the page to format it, run the free check and compare the rewrite.

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-formatter.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-formatter"}' | 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": "rewrite", "sql": "...", "dialect": "postgresql", "request": "", "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-formatter:rewrite:$(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, sql, same_results, result_change, changes, kept, notes}
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-formatter:rewrite:$(shasum -a 256 input.json | cut -c1-16):a1" --data @input.json