CartoDB-SQL-API/doc/making_calls.md

191 lines
5.9 KiB
Markdown
Raw Normal View History

# Making Calls to the SQL API
2015-10-22 17:43:14 +08:00
CARTO is based on the rock solid PostgreSQL database. All of your tables reside a single database, which means you can perform complex queries joining tables, or carrying out geospatial operations. The best place to learn about PostgreSQL's SQL language is the [official documentation](http://www.postgresql.org/docs/9.1/static/).
2015-10-22 17:43:14 +08:00
CARTO is also based on PostGIS, so take a look at the [official PostGIS reference](http://postgis.refractions.net/docs/) to know what functionality we support in terms of geospatial operations. All of our tables include a column called *the_geom,* which is a geometry field that indexes geometries in the EPSG:4326 (WGS 1984) coordinate system. All tables also have an automatically generated and updated column called *the_geom_webmercator*. We use the column internally to quickly create tiles for maps.
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
2015-10-22 17:43:14 +08:00
## URL endpoints
All SQL API requests to your CARTO account should follow this general pattern:
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
#### SQL query example
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?q={SQL statement}
2015-10-22 17:43:14 +08:00
```
If you encounter errors, double-check that you are using the correct account name, and that your SQL statement is valid. A simple example of this pattern is conducting a count of all the records in your table:
2015-10-28 21:31:42 +08:00
#### Count example
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?q=SELECT count(*) FROM {table_name}
2015-10-22 17:43:14 +08:00
```
2015-10-28 21:31:42 +08:00
#### Result
2015-10-22 17:43:14 +08:00
```javascript
{
time: 0.007,
total_rows: 1,
rows: [
{
count: 4994
}
]
}
```
2016-05-26 06:43:30 +08:00
Finally, remember that in order to use the SQL API, either your table must be public, or you must be [authenticated](http://docs.carto.com/carto-engine/sql-api/authentication/#authentication) using API Keys.
2015-10-22 17:43:14 +08:00
## POST and GET
The CARTO SQL API is setup to handle both GET and POST requests. You can test the GET method directly in your browser. Below is an example of a jQuery SQL API request to CARTO:
2015-10-28 21:31:42 +08:00
### jQuery
#### Call
2015-10-22 17:43:14 +08:00
```javascript
$.getJSON('https://{username}.carto.com/api/v2/sql/?q='+sql_statement, function(data) {
2015-10-22 17:43:14 +08:00
$.each(data.rows, function(key, val) {
// do something!
});
});
```
By default, GET requests work from anywhere. In CARTO, POST requests work from any website as well. We achieve this by hosting a cross-domain policy file at the root of all of our servers. This allows you the greatest level of flexibility when developing your application.
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
2015-10-22 17:43:14 +08:00
## Response formats
The standard response from the CARTO 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.
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
### JSON
#### Call
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?q=SELECT * FROM {table_name} LIMIT 1
2015-10-22 17:43:14 +08:00
```
2015-10-28 21:31:42 +08:00
#### Result
2015-10-22 17:43:14 +08:00
```javascript
{
time: 0.006,
total_rows: 1,
rows: [
{
year: " 2011",
month: 10,
day: "11",
the_geom: "0101000020E610...",
cartodb_id: 1,
2015-10-22 17:43:14 +08:00
the_geom_webmercator: "0101000020110F000..."
}
]
}
```
Alternatively, you can use the [GeoJSON specification](http://www.geojson.org/geojson-spec.html) to return data from the API. To do so, simply supply the `format` parameter as GeoJSON:
2015-10-28 21:31:42 +08:00
### GeoJSON
#### Call
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM {table_name} LIMIT 1
2015-10-22 17:43:14 +08:00
```
2015-10-28 21:31:42 +08:00
#### Result
2015-10-22 17:43:14 +08:00
```javascript
{
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: {
year: " 2011",
month: 10,
day: "11",
cartodb_id: 1
2015-10-22 17:43:14 +08:00
},
geometry: {
type: "Point",
coordinates: [
-97.335,
35.498
]
}
}
]
}
```
The SQL API accepts other output formats that can be useful to export data. You can use the following formats: CSV, SHP, SVG, KML, SpatiaLite and GeoJSON.
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
2015-10-22 17:43:14 +08:00
## Output filename
2015-10-28 21:31:42 +08:00
2015-10-22 17:43:14 +08:00
To customize the output filename, add the `filename` parameter to your URL:
2015-10-28 21:31:42 +08:00
#### Call
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?filename={custom_filename}&q=SELECT * FROM {table_name} LIMIT 1
2015-10-22 17:43:14 +08:00
```
2015-10-28 21:31:42 +08:00
2015-10-22 17:43:14 +08:00
## Getting table information
Currently, there is no public method to access your table schemas. The simplest way to retrieve table structure is to access the first row of the data,
2015-10-28 21:31:42 +08:00
#### Call
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?q=SELECT * FROM {table_name} LIMIT 1
2015-10-22 17:43:14 +08:00
```
2015-10-28 21:31:42 +08:00
2015-10-22 17:43:14 +08:00
## Response errors
To help you debug your SQL queries, the CARTO SQL API returns the full error provided by PostgreSQL, as part of the JSON response. Error responses appear in the following format,
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
#### Result
2015-10-22 17:43:14 +08:00
```javascript
{
error: [
"syntax error at or near "LIMIT""
]
}
```
You can use these errors to help understand your SQL. If you encounter errors executing SQL, either through the CARTO Editor, or through the SQL API, it is suggested to Google search the error for independent troubleshooting.
2015-10-28 21:31:42 +08:00
## Write data to your CARTO account
2015-10-22 17:43:14 +08:00
Performing inserts or updates on your data is simple using your [API Key](#authentication). All you need to do is supply a correct SQL [INSERT](http://www.postgresql.org/docs/9.1/static/sql-insert.html) or [UPDATE](http://www.postgresql.org/docs/9.1/static/sql-update.html) 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).
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
### Insert
#### Call
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?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={api_key}
2015-10-22 17:43:14 +08:00
```
Updates are just as simple. Here is an example of updating a row based on the value of the cartodb_id column.
2015-10-22 17:43:14 +08:00
2015-10-28 21:31:42 +08:00
### Update
#### Call
2015-10-22 17:43:14 +08:00
```bash
https://{username}.carto.com/api/v2/sql?q=UPDATE test_table SET column_name = 'my new string value' WHERE cartodb_id = 1 &api_key={api_key}
2015-10-22 17:43:14 +08:00
```