Skip to content

SQL Formatter API

Overview

Formats SQL input and can rewrite SQL for a different dialect. Body mode runs synchronously. URL and pre-uploaded file modes run asynchronously.

Endpoint

Property Value
Method POST
URL https://api.apidatatools.com/sql-formatter-api

Authentication

Include your API key in the x-api-key header:

x-api-key: YOUR_API_KEY

Input modes

Select a mode with x-source-type:

Mode Behavior
body Formats raw SQL text synchronously. This is the default.
url Processes a SQL file at a URL asynchronously.
file Processes a pre-uploaded file asynchronously.

Headers

Header names are case-insensitive. The spellings below are lowercase.

Header Type Required Default / values Description
x-api-key String Yes Your API key Authenticates the request.
x-source-type String No Default: body. Values: body, url, file Selects the input mode.
x-format-mode String No Default: pretty. Values: pretty, rewrite Selects formatting behavior. rewrite requires x-output-sql-dialect.
x-input-sql-dialect String No No fixed default Specifies the input SQL dialect. If omitted, the service may detect MySQL dump syntax; otherwise dialect handling is delegated to the SQL parser.
x-output-sql-dialect String Required for rewrite mode; otherwise no No fixed default Specifies the target SQL dialect for rewrite mode. Use a dialect supported by the service.

Accepted file extensions

For url and file modes, the accepted extensions are .sql and .txt.

Request body

For body mode, send the SQL as raw text, not as a JSON object.

For url mode, send a JSON object containing the URL:

{
  "url": "https://example.com/input.sql"
}

For file mode, send a JSON object containing the exact storage key returned by the upload workflow:

{
  "file": "S3_KEY_RETURNED_BY_THE_UPLOAD_WORKFLOW"
}

curl examples

Body mode

curl -X POST "https://api.apidatatools.com/sql-formatter-api" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "x-source-type: body" \
  -H "Content-Type: text/plain" \
  --data-raw "select id,name from customers where active = 1;"

URL mode

curl -X POST "https://api.apidatatools.com/sql-formatter-api" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "x-source-type: url" \
  -H "Content-Type: application/json" \
  --data-raw '{"url":"https://example.com/input.sql"}'

Pre-uploaded file mode

curl -X POST "https://api.apidatatools.com/sql-formatter-api" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "x-source-type: file" \
  -H "Content-Type: application/json" \
  --data-raw '{"file":"S3_KEY_RETURNED_BY_THE_UPLOAD_WORKFLOW"}'

Success responses

Synchronous body mode

A successful response has HTTP 200 and includes a preview of the formatted SQL:

{
  "request_id": "REQUEST_ID",
  "status": "success",
  "file": "https://downloads.apidatatools.com/OUTPUT_FILE",
  "preview": "SELECT\n  id,\n  name\nFROM customers\nWHERE active = 1\n"
}

The file value is a temporary download URL. Its retention period depends on your plan.

Asynchronous URL and file modes

A successful job submission has HTTP 202:

{
  "status": "accepted",
  "job_id": "JOB_ID",
  "status_url": "https://api.apidatatools.com/jobs/JOB_ID",
  "request_id": "REQUEST_ID"
}

Async job status

Check a job with GET https://api.apidatatools.com/jobs/{job_id}. Authenticate with x-api-key. The status response includes request_id, job_id, status, created_at, and updated_at.

For a queued or processing job, the response also includes:

{
  "message": "Your job is being processed.",
  "retry_after": 2
}

For a successful job, the response includes result:

{
  "request_id": "REQUEST_ID",
  "job_id": "JOB_ID",
  "status": "success",
  "created_at": 0,
  "updated_at": 0,
  "result": {
    "status": "success",
    "file": "https://downloads.apidatatools.com/OUTPUT_FILE",
    "preview": "SELECT\n  id\nFROM customers\n"
  }
}

For a failed job, the response includes error:

{
  "request_id": "REQUEST_ID",
  "job_id": "JOB_ID",
  "status": "failed",
  "created_at": 0,
  "updated_at": 0,
  "error": {
    "code": "SQL_SYNTAX_ERROR",
    "message": "Syntax error in SQL.",
    "details": {
      "error": "SQL parser error details"
    }
  }
}

The created_at and updated_at values are integer timestamps. Job status results use the status endpoint’s response shape; processing failures are reported in the failed job’s error object.

Error handling

Errors use this envelope. details may contain additional fields; details.message contains the error message.

{
  "status": "error",
  "error": "ERROR_CODE",
  "request_id": "REQUEST_ID",
  "details": {
    "message": "Description of the error"
  }
}

The following HTTP statuses are established by the service:

HTTP status Error codes / cases
400 EMPTY_BODY, INVALID_BODY_ENCODING, INVALID_SOURCE_TYPE, EMPTY_SQL, INVALID_UTF8, INVALID_FORMAT_MODE, MISSING_OUTPUT_DIALECT, UNSUPPORTED_DIALECT, FORMAT_FAILED, SQL_SYNTAX_ERROR, INTERNAL_FORMATTER_ERROR, MISSING_INPUT_FILE, INVALID_SQL, INVALID_BODY, INVALID_URL, URL_UNREACHABLE, URL_NOT_OK, INVALID_FILE, FILE_UNAVAILABLE, INVALID_FILE_EXTENSION, INVALID_JOB_ID, JOB_NOT_FOUND, FORBIDDEN, DDB_ERROR
401 UNAUTHORIZED
404 USER_NOT_FOUND, UNKNOWN_FUNCTION
413 PAYLOAD_TOO_LARGE, FILE_TOO_LARGE
429 QUOTA_EXCEEDED, RATE_LIMITED
500 PLAN_NOT_FOUND, INTERNAL_ERROR, CONFIG_ERROR, PLAN_CONFIG_ERROR

Async job processing can also fail with INVALID_JOB, DOWNLOAD_FAILED, UPLOAD_FAILED, or INTERNAL_ERROR. These appear as the error code in the failed job status response; they are not HTTP statuses for the original job-submission request.

Notes

  • Body mode is synchronous; URL and file modes return an accepted job for asynchronous processing.
  • Body-size, file-size, quota, rate, and download-retention limits depend on your plan. See tool limits.
  • Download URLs are temporary; retention depends on your plan.