2012-07-24 16:30:57 +08:00
SQL API
=======
2012-07-24 16:58:26 +08:00
Request format
--------------
2012-07-24 16:30:57 +08:00
2012-07-24 16:58:26 +08:00
Supported query string parameters:
2012-07-24 16:30:57 +08:00
2012-07-24 16:58:26 +08:00
'q': Specifies the SQL query to run
Example:
'http://entrypoint?q=SELECT count(*) FROM mytable'
2012-07-24 16:30:57 +08:00
2012-07-24 16:58:26 +08:00
'format': Specifies which format to use for the response.
Supported formats: JSON (the default), GeoJSON,
2013-11-19 18:20:12 +08:00
TopoJSON, CSV, SVG, SHP, arraybuffer
2012-07-24 16:30:57 +08:00
2012-11-12 20:21:46 +08:00
'filename': Sets the filename to use for the query result
file attachment
2012-11-13 00:10:16 +08:00
'skipfields':
Comma separate list of fields that are not wanted
in output. Only useful with "SELECT *" queries.
2012-07-24 16:58:26 +08:00
'dp': Number of digits after the decimal point.
2013-01-10 00:43:23 +08:00
Only affects format GeoJSON, TopoJSON, SVG.
2012-07-24 16:58:26 +08:00
By default this is 6.
2012-07-24 16:30:57 +08:00
2012-07-24 16:58:26 +08:00
'api_key': Needed to authenticate in order to modify the database.
2012-07-24 16:30:57 +08:00
2012-11-13 02:14:20 +08:00
'cache_policy':
Set to "persist" to have the server send an Cache-Control
header requesting caching devices to keep the response
cached as much as possible. This is best used with a
timestamp value in cache_buster for manual control of
updates.
2013-12-03 23:52:55 +08:00
'rows_per_page':
Limit the number of rows in output. Affects all formats,
see also 'page'.
'page':
When 'rows_per_page' is used to limit rows, 'page' can be
used to specify which page to start returning rows from.
First page has index 0.
2014-04-14 21:01:12 +08:00
'order_by':
Causes the result rows to be sorted according to the specified
case sensitive column name. See also 'sort_order'.
'sort_order':
Optional param combined with 'order_by' one. Values are limited
to ASC (ascending) and DESC (descending), case insensitive. If
not specified or wrongly specified, ASC is assumed by default.
2012-07-24 16:30:57 +08:00
Response formats
----------------
2013-11-19 18:20:12 +08:00
### JSON
2012-07-24 16:30:57 +08:00
The standard response from the CartoDB SQL API is JSON. If you are
building a web-application, the lightweight JSON format allows you to
quickly integrate data from the SQL API.
2012-07-24 16:58:26 +08:00
The JSON response is as follows:
2012-07-24 16:30:57 +08:00
```
{
time: 0.006,
total_rows: 1,
2013-06-15 00:38:31 +08:00
fields: {
'year': { type:'string' },
'the_geom': { type:'geometry' },
'cartodb_id': { type:'number' },
'created_at': { type:'date' },
'updated_at': { type:'date' }
2013-08-22 19:44:21 +08:00
'valid': { type:'boolean' }
2013-06-15 00:38:31 +08:00
},
2012-07-24 16:30:57 +08:00
rows: [
{
year: " 2011",
the_geom: "0101000020E610...",
cartodb_id: 1,
created_at: "2012-02-06T22:50:35.778Z",
2013-08-22 19:44:21 +08:00
updated_at: "2012-02-12T21:34:08.193Z",
valid: true
2012-07-24 16:30:57 +08:00
}
2013-11-19 00:01:06 +08:00
],
notices: [ 'notice1', 'notice2' ], // optional
warnings: [ 'warning1', 'warning2' ] // optional
2012-07-24 16:30:57 +08:00
}
```
2013-11-19 18:20:12 +08:00
Note that values of numerical fields will be rounded to at most 16 significant
digits due to a limit in the JSON format itself. If more precision is needed
the caller can cast those fields to text using SQL constructs.
### GeoJSON
2012-07-24 16:30:57 +08:00
Alternatively, you can use the GeoJSON specification for returning data
2012-07-24 16:58:26 +08:00
from the API. To do so, simply supply the format parameter as GeoJSON.
2012-07-24 16:30:57 +08:00
2012-07-24 16:58:26 +08:00
The GeoJSON response is follows:
2012-07-24 16:30:57 +08:00
```
{
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: {
year: " 2011",
month: 10,
day: "11",
cartodb_id: 1,
created_at: "2012-02-06T22:50:35.778Z",
updated_at: "2012-02-12T21:34:08.193Z"
},
geometry: {
type: "Point",
coordinates: [
-97.335,
35.498
]
}
}
]
}
```
2013-11-19 18:20:12 +08:00
### CSV
CSV export format has field names in first line (header),
2013-11-20 20:45:36 +08:00
uses CR/NL line endings and HEXEWKB format for geometry fields.
2013-11-19 18:20:12 +08:00
2013-11-20 20:45:36 +08:00
See https://github.com/postgis/postgis/blob/svn-trunk/doc/ZMSgeoms.txt#L42
2013-11-19 18:20:12 +08:00
### KML
See http://en.wikipedia.org/wiki/KML
### SVG
See http://en.wikipedia.org/wiki/SVG
### TopoJSON
See http://en.wikipedia.org/wiki/Topojson
### SHP
Shapefile output composed by .shp, .shx, .dbf and .prj files
compressed in a .zip.
See http://en.wikipedia.org/wiki/Shapefile
### Arraybuffer
An octet stream binary-encoding each field as a typed array of value.
TODO: write more about this format
See http://github.com/CartoDB/CartoDB-SQL-API/issues/96
2012-07-24 16:30:57 +08:00
Response errors
---------------
To help you debug your SQL queries, the CartoDB SQL API returns errors
as part of the JSON response. Errors come back as follows,
```
{
error: [
"syntax error at or near "LIMIT""
]
}
```
2012-07-24 17:03:32 +08:00
You can use these errors to help understand your SQL.
2012-07-24 16:30:57 +08:00
2012-07-24 16:58:26 +08:00
Getting table information
-------------------------
Currently, there is no public method for accessing your table schemas. The
2012-07-24 17:03:32 +08:00
simplest way to get table structure is to access the first row of the data:
2012-07-24 16:58:26 +08:00
http://entrypoint?q=SELECT * FROM mytable LIMIT 1
2012-07-24 16:30:57 +08:00
Write data to your CartoDB account
----------------------------------
Perform inserts or updates on your data is simple now using your API
key. All you need to do, is supply a correct SQL INSERT or UPDATE
statement for your table along with the api_key parameter for your
account. Be sure to keep these requests private, as anyone with your API
key will be able to modify your tables. A correct SQL insert statement
means that all the columns you want to insert into already exist in
your table, and all the values for those columns are the right type
(quoted string, unquoted string for geoms and dates, or numbers).
INSERT
2012-07-24 16:58:26 +08:00
http://entrypoint?q=INSERT INTO test_table (column_name, column_name_2, the_geom) VALUES ('this is a string', 11, ST_SetSRID(ST_Point(-110, 43),4326))& api_key={Your API key}
2012-07-24 16:30:57 +08:00
Updates are just as simple. Here is an example, updating a row based on
the value of the cartodb_id column.
UPDATE
2012-07-24 16:58:26 +08:00
http://entrypoint?q=UPDATE test_table SET column_name = 'my new string value' WHERE cartodb_id = 1 & api_key={Your API key}
2012-07-24 16:30:57 +08:00