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 = urllub2.urlopen('http://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 Ruby:

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

uri = URI("http://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.

Please consider a small donation to keep this service up and running if you plan to use this API on a regular basis.
What happens with donations?