Interact with your tables and data inside CARTO, as if you were running SQL statements.

This component is still under support but it will not be further developed. We don’t recommend starting new projects with it as it will eventually become deprecated. Instead, learn more about our new APIs here

Introduction

CARTO’s SQL API allows you to interact with your tables and data inside CARTO, as if you were running SQL statements against a normal database.

You can execute single SQL statements or even a batch of long-running ones. Refer to the SQL API guide to learn more.

Authorization

In order to access SQL API you must provide an API key. The CARTO Authorization guide explains how these keys are sent (TLDR: HTTP basic auth or query string param with the API key token). Depending on the permissions granted to the provided API key, the request will be authorized or not.

Download OpenAPI specificationHave you found an error? Github issues

Single SQL Statement

Run a single SQL statement

Using GET

get /sql
Production server (uses live data)
https://{user}.{domain}/api/v2/sql

Runs a single SQL statement using the GET endpoint:

  • SELECT, INSERT, UPDATE, DELETE,
  • CREATE TABLE, ALTER TABLE, DROP TABLE
  • CREATE INDEX

NOTICE: If the database detects an error when it's already streaming data the status code will be 200. Make sure to check if the optional error property is set in the response.

Authorizations:
query Parameters
q
required
string (SQL statement)

SQL statement

filename
string

Output filename

format
string (Output format)
Enum:"GPKG" "CSV" "SHP" "SVG" "KML" "SpatiaLite" "GeoJSON"

Output format

Responses
200

Ok (Check if error property is present)

400

Bad Request

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

429

You are over platform's limits.

Request samples
curl -X GET \
https://username.carto.com/api/v2/sql?q=SELECT count(*) FROM cities
Click to Copy
Response samples
application/json
{
  • "time": 0.007,
  • "total_rows": 1,
  • "rows":
    [
    ]
}
Click to Copy

Using POST

post /sql
Production server (uses live data)
https://{user}.{domain}/api/v2/sql

Runs a single SQL statement using the POST endpoint:

  • SELECT, INSERT, UPDATE, DELETE,
  • CREATE TABLE, ALTER TABLE, DROP TABLE
  • CREATE INDEX

Offers the same functionality as the GET endpoint. This version may come handy when dealing with complex/long statments.

NOTICE: If the database detects an error when it's already streaming data the status code will be 200. Make sure to check if the optional error property is set in the response.

Authorizations:
Request Body schema: application/json
q
required
string (SQL statement)
filename
string

Output filename

format
string (Output format)
Enum:"GPKG" "CSV" "SHP" "SVG" "KML" "SpatiaLite" "GeoJSON"

Output format

Responses
200

Ok (Check if error property is present)

400

Bad Request

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

429

You are over platform's limits.

Request samples
application/json
{
  • "q": "SELECT count(*) FROM cities",
  • "filename": "number_of_cities.json"
}
Click to Copy
Response samples
application/json
{
  • "time": 0.007,
  • "total_rows": 1,
  • "rows":
    [
    ]
}
Click to Copy

Batch Queries

A Batch Queries Job enables you to request statements with long-running CPU processing times

Create a Job

post /sql/job
Production server (uses live data)
https://{user}.{domain}/api/v2/sql/job

Creates a Batch Queries Job

Authorizations:
Request Body schema: application/json
query
required
string or string or object

long-running SQL statement(s).

Responses
200

Ok

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

429

You are over platform's limits.

Request samples
application/json
{
  • "query": "UPDATE nasdaq SET price = '$101.00' WHERE company = 'CARTO'"
}
Click to Copy
Response samples
application/json
{
  • "job_id": "de305d54-75b4-431b-adb2-eb6b9e546014",
  • "user": "username",
  • "status": "pending",
  • "query": "UPDATE nasdaq SET price = '$101.00' WHERE company = 'CARTO'",
  • "created_at": "2017-12-15T07:36:25Z",
  • "updated_at": "2017-12-15T07:36:25Z"
}
Click to Copy

Get a Job

get /sql/job/{job_id}
Production server (uses live data)
https://{user}.{domain}/api/v2/sql/job/{job_id}

Returns a Batch Queries Job based on it's ID.

Authorizations:
path Parameters
job_id
required
string <uuid>

the job universally unique identifier (uuid).

Responses
200

Ok

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

404

The specified resource was not found

429

You are over platform's limits.

Request samples
curl -X GET \
https://username.carto.com/api/v2/sql/job/de305d54-75b4-431b-adb2-eb6b9e546014
Click to Copy
Response samples
application/json
{
  • "job_id": "de305d54-75b4-431b-adb2-eb6b9e546014",
  • "user": "username",
  • "status": "pending",
  • "query": "UPDATE nasdaq SET price = '$101.00' WHERE company = 'CARTO'",
  • "created_at": "2017-12-15T07:36:25Z",
  • "updated_at": "2017-12-15T07:36:25Z"
}
Click to Copy

Update a Job

put /sql/job/{job_id}
Production server (uses live data)
https://{user}.{domain}/api/v2/sql/job/{job_id}

Updates the query of a Batch Queries Job.

Notice: Only the query property can be updated

Authorizations:
path Parameters
job_id
required
string <uuid>

the job universally unique identifier (uuid).

Request Body schema: application/json
query
required
string or string or object

long-running SQL statement(s).

Responses
200

Ok

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

404

The specified resource was not found

429

You are over platform's limits.

Request samples
application/json
{
  • "q": "UPDATE nasdaq SET price = '$999.00' WHERE company = 'CARTO'"
}
Click to Copy
Response samples
application/json
{
  • "job_id": "de305d54-75b4-431b-adb2-eb6b9e546014",
  • "user": "username",
  • "status": "pending",
  • "query": "UPDATE nasdaq SET price = '999.00' WHERE company = 'CARTO'",
  • "created_at": "2017-12-15T07:36:25Z",
  • "updated_at": "2017-12-16T12:52:13Z"
}
Click to Copy

Cancel a Job

delete /sql/job/{job_id}
Production server (uses live data)
https://{user}.{domain}/api/v2/sql/job/{job_id}

Canceles a Batch Queries Job based on it's ID. The Job doesn't get deleted, just it's status is set as canceled. Only jobs whose status are pending or running can be canceled.

  • pending: the job will never be executed
  • running: the job will be terminated immediately
Authorizations:
path Parameters
job_id
required
string <uuid>

the job universally unique identifier (uuid).

Responses
200

Ok

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

404

The specified resource was not found

429

You are over platform's limits.

Request samples
curl -X DELETE \
https://username.carto.com/api/v2/sql/job/de305d54-75b4-431b-adb2-eb6b9e546014
Click to Copy
Response samples
application/json
{
  • "job_id": "de305d54-75b4-431b-adb2-eb6b9e546014",
  • "user": "username",
  • "status": "canceled",
  • "query": "UPDATE nasdaq SET price = '999.00' WHERE company = 'CARTO'",
  • "created_at": "2017-12-15T07:36:25Z",
  • "updated_at": "2017-12-16T12:52:13Z"
}
Click to Copy

Single COPY command

Runs a copy command to ingest data

post /sql/copyfrom
Production server (uses live data)
https://{user}.{domain}/api/v2/sql/copyfrom

Runs a single COPY command:

  • COPY mytable (col1, col2) FROM stdin WITH (FORMAT CSV)
Authorizations:
query Parameters
q
required
string (SQL statement)
Example: "COPY upload_example (the_geom,name,age) FROM stdin WITH (FORMAT csv,HEADER true)"

COPY statement

header Parameters
Transfer-Encoding
required
string
Enum:"chunked"
Content-Encoding
string
Enum:"gzip"
Request Body schema: application/octet-stream
string
Responses
200

Ok

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

429

You are over platform's limits.

Request samples
curl -X POST -H 'Content-Encoding: gzip' -H 'Transfer-Encoding: chunked' -H 'Content-Type: application/octet-stream' --data-binary @upload_example.csv.gz 'https://{username}.carto.com/api/v2/sql/copyfrom?q=COPY upload_example (the_geom,name,age) FROM stdin WITH (FORMAT csv,HEADER true)&api_key={api_key}'
Click to Copy
Response samples
application/json
{
  • "time": 2.3,
  • "total_rows": 142012
}
Click to Copy

Runs a copy command to extract data

get /sql/copyto
Production server (uses live data)
https://{user}.{domain}/api/v2/sql/copyto

Runs a single COPY command:

  • COPY mytable TO stdout WITH (FORMAT CSV)
Authorizations:
query Parameters
q
required
string (SQL statement)

COPY statement

filename
string

Sets the content-disposition file name header

Responses
200

Ok.

401

Unauthorized. No authentication provided.

403

Forbidden. The API key does not authorize this request.

429

You are over platform's limits.

Request samples
curl --output upload_example_dl.csv --compressed  "https://{username}.carto.com/api/v2/sql/copyto?q=COPY upload_example (the_geom,name,age) TO stdout WITH(FORMAT csv,HEADER true)&api_key={api_key}"
Click to Copy