Back to SQL Formatter
Developer Documentation

SQL Formatter API Reference

Integrate AST-level SQL formatting into CI/CD pipelines, pre-commit hooks, IDE plugins, and backend services using the Connect RPC JSON API.

Endpoint Information

POSThttps://api.leuduan.work/sqlformat.v1.SqlFormatService/FormatSql
ProtocolConnect Protocol v1 (JSON over HTTP/POST)
AuthenticationPublic (No API key required for standard use)

Required Request Headers

Header NameRequired ValueDescription
Content-Typeapplication/jsonPayload content format
Connect-Protocol-Version1Specifies Connect RPC protocol specification version

Request Body Parameters

FieldTypeRequiredDescription
sqlstringYesThe raw SQL query or multi-statement script to format.
dialectstringYesTarget dialect: bigquery, postgresql, mysql, mssql, clickhouse, snowflake, duckdb, sqlite, generic
optionsobjectNoFormatting configuration object (details below).
options.max_lengthnumberNoMaximum target line length before wrapping (default: 120).
options.keyword_handlingstringNoTEXT_CASE_UPPER_CASE or TEXT_CASE_LOWER_CASE
options.builtin_function_handlingstringNoTEXT_CASE_UPPER_CASE or TEXT_CASE_LOWER_CASE
options.indent_cte_definitionsbooleanNoIndent common table expression bodies (default: true).
options.googlesql.always_break_pipebooleanNoBreak each GoogleSQL pipe (|>) onto a newline (default: true).

Response Body & Errors

Response FieldTypeStatus / PresenceDescription
formatted_sqlstring200 OK (Success)The formatted SQL output text with canonical indentation and applied options.
dialectstring200 OK (Success)The canonical dialect identifier used for formatting (e.g. bigquery, postgresql).
codestring4xx / 5xx (Error)Connect error code (e.g. invalid_argument, internal).
messagestring4xx / 5xx (Error)Human-readable error description, including syntax error locations (line and column) when parsing fails.

Code Integration Examples

cURL Request
curl -sS -X POST "https://api.leuduan.work/sqlformat.v1.SqlFormatService/FormatSql" \
  -H "Content-Type: application/json" \
  -H "Connect-Protocol-Version: 1" \
  -d '{
    "dialect": "bigquery",
    "sql": "SELECT c.id, c.name, SUM(o.amount) as total FROM `my_project.analytics.customers` c JOIN `my_project.analytics.orders` o ON c.id = o.customer_id GROUP BY c.id, c.name HAVING total > 1000",
    "options": {
      "max_length": 100,
      "keyword_handling": "TEXT_CASE_UPPER_CASE",
      "builtin_function_handling": "TEXT_CASE_LOWER_CASE",
      "indent_cte_definitions": true,
      "always_break_select": false,
      "always_break_query": true,
      "googlesql": {
        "always_break_pipe": true
      }
    }
  }' | jq -r '.formatted_sql'

Sample JSON Response

HTTP 200 OK
{
  "formatted_sql": "SELECT\n  c.id,\n  c.name,\n  sum(o.amount) AS total\nFROM `my_project.analytics.customers` AS c\nJOIN `my_project.analytics.orders` AS o\n  ON c.id = o.customer_id\nGROUP BY c.id, c.name\nHAVING total > 1000;",
  "dialect": "bigquery"
}

Try the Interactive Formatter

Experiment with queries, options, and live Git diffs in the web browser.

Open SQL Formatter