API Documentation

This service provides an API that makes it easy to integrate sqlformat with your own website or your application.

The returned data is transferred as JSON objects, except for the response body of error messages which are in plain text.

HTTP Status Codes

The API uses the following status codes:

HTTP Status Description
200 Everything's fine
400 Invalid request. For some reason the server refused to process the request. This is most likely because of invalid or malformed data send to the server. The body of the response contains an error message.
429 Usage limit exceeded. This happens when too many requests from the same IP reached the server. See below for details.
500 Something went incredibly wrong. If you see this status code, please contact me if you see this error.

API Functions

The service exposes the following endpoints:

Format SQL statements

URL
/api/v1/format
Method
POST
Parameter
Name Description
sql Required. The SQL statement as string.
reindent Set to 1 to reindent the statement with default settings (default: 0).
indent_width An integer to set the indendation with (default: 2).
identifier_case How to change identifiers. Valid values are 'upper', 'lower', 'capitalize'. The default is to leave identifiers unchanged.
keyword_case How to change keywords. Valid values are 'upper', 'lower', 'capitalize'. The default is to leave identifiers unchanged.
strip_comments Set to 1 to remove comments from the statement (default: 0).
Response
The response object has a single key "result" which holds the formatted SQL statement.

Example request and response body:

# POST parameter in request
reindent=1&sql=select+%2A+from+foo

# Response body
{"result": "select *\nfrom foo"}
  

Split SQL statements

URL
/api/v1/split
Method
POST
Parameter
Name Description
sql Required. The SQL statements as a single string.
Response
The response object has a single key "result" which holds a list of SQL statements.

Example request and response body:

# POST parameter in request
sql=select+%2A+from+foo%3B+select+%2A+from+bar

# Response body
{"result": ["select * from foo;", "select * from bar"]}
  

Usage Limit

For now access to the API is limited to 500 requests per hour for a single IP. Please contact me if you hit this limit.

Examples

Using the API from a Python script:

>>> import urllib2, urllib
>>> import json
>>> params = {'sql': 'select * from foo', 'reindent': 1}
>>> response = urllib2.urlopen('https://sqlformat.org/api/v1/format', data=urllib.urlencode(params))
>>> data = json.loads(response.read())
>>> print data['result']
select *
from foo
>>>

Using the API from JavaScript: http://jsfiddle.net/andialbrecht/jq4wd/.

Using the API from Bash:

#!/bin/bash

#
# author: Michał Skrzyński (skrzynski.michal@gmail.com)
# version: 1.0.2 (28.11.2019)
#

[ ! -x /usr/bin/curl ] && { echo 'Error: curl is not installed.' >&2 ;exit 1; }
[ ! -x /usr/bin/jq ] && { echo 'Error: jq is not installed.' >&2 ;exit 1; }

[[ -f "${1}" || -p /dev/stdin ]] && { sql=$(cat "${1:--}"); } || { sql="${1}"; }

curl -s -X POST -d "reindent=1&sql=${sql}" https://sqlformat.org/api/v1/format|jq -r .result

Thanks to Michał Skrzyński for sharing this script.

Using the API from Ruby:

require 'uri'
require 'net/http'
require 'json'

uri = URI("https://sqlformat.org/api/v1/format")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = false

body = URI.encode_www_form({:sql => "select * from foo", :reindent => 1, :identifier_case => "upper"})

response = https.post(uri.path, body)
puts JSON.parse(response.body)["result"]

Thanks to Shiva Huang for the Ruby example.

Using the API from Powershell

# Payload in hashtable
$body = @{
    sql            = '/* A comment */ seLEecT * from foo where fieldA = 22 and fieldC = "Something" or fieldB=99'
    reindent       = 1
    indent_width   = 3
    # identifier_case   ="upper"
    keyword_case   = "upper"
    strip_comments = 1
}

# Prepare hashtable to be used in the invocation
$params = @{
    Uri         = 'https://sqlformat.org/api/v1/format'
    Method      = 'POST'
    Body        = $body
    ContentType = "application/x-www-form-urlencoded"
}

# Invoke using hashtables
$response = Invoke-RestMethod @params
write-host $response.result

# OR
# Invoke using only the payload in the hashtable
$response = Invoke-RestMethod -Uri "https://sqlformat.org/api/v1/format" -method Post -ContentType "application/x-www-form-urlencoded" -Body $body
write-host $response.result

Result then looks like:
SELECT *
FROM foo
WHERE fieldA = 22
   AND fieldC = "Something"
   OR fieldB=99

Thanks to René Hug for the Powershell example.