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-01-10 00:43:23 +08:00
TopoJSON, CSV, SVG, SHP
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.
2012-07-24 16:30:57 +08:00
Response formats
----------------
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
}
]
}
```
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-01-10 00:43:23 +08:00
TODO: csv, kml, svg, topojson responses
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