Spatial Analysis in 2025: Key Trends Report
| Download Now
CARTO
Platform
Overview
Visualization
Analytics
App Development
Data Enrichment
AI Agents
Enterprise & Security
Pricing
CARTO for
Google Cloud
AWS
Azure
Snowflake
Databricks
Oracle
Solutions
By Industry
By Use Case
By Role
Telecoms
Icon/indus/Insurance-menu
Icon/indus/Insurance-menu-mobile
Insurance
Logistics
Real Estate
Financial Services
Retail
Marketing & Advertising
Mobility
All industries
Network Deployment
Catastrophe Modeling
Fraud Detection
Market Analysis
Environmental Management
Site Selection
Geomarketing
Data Monetization
IoT Analytics
Supply Chain Optimization
Healthcare Analytics
Territory Planning
All use cases
Data Analyst
Data Scientist
GIS Professional
Developer
Customers
Resources
Learn
Documentation
CARTO Academy
Blog
Reports
Glossary
Connect
Events
Partners
Webinars
Log inRequest a demo
Try for free
2050-01-01
1990-01-01
"ES", "GB"

This content applies to a previous version of CARTO

In October 2021 we released a new version of our platform. You can find the latest documentation at docs.carto.com

Back to Help Center

How to Use the CARTO SQL API from a CARTO.js or CARTO-VL Application

Overview

The CARTO Developer Center provides detailed guidance on making calls to the CARTO SQL API. You can utilize several approaches to send HTTP requests from CARTO.js applications, including the JavaScript Fetch API, Axios, or jQuery Ajax.

JavaScript Fetch API

Note that Fetch API support varies across browsers. Here’s an implementation example:

// request to CARTO account using the Fetch API
fetch(`
    https://cartojs-test.carto.com/api/v2/sql?q=SELECT COUNT(*) FROM ne_10m_populated_places_simple`
)
// we transform the response from the Fetch API into a JSON format
.then((resp) => resp.json())

.then((response) => {
    // we get the data from the request response
    console.log(response.rows[0])
})
.catch(function (error) {
    // check if the request is returning an error
    console.log(error)
});

External JavaScript Libraries

For browsers lacking Fetch API support, libraries like Axios provide alternatives:

// load axios library
// <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
let SQL_CLIENT = axios.create({
    method: 'get',
    url: 'https://cartojs-test.carto.com/api/v2/sql'
});

/* make a request and put callbacks for success and error events */
SQL_CLIENT.request({
        params: {
            q: "SELECT COUNT(*) FROM ne_10m_populated_places_simple"
        },
    })
    .then(function(response){
        // we get the data from the request response
        console.log(response.data.rows[0])
    })
    .catch(function (error) {
        // check if the request is returning an error
        console.log(error)
    });

Authentication

Public and publicly-linked datasets permit read operations like SELECT without API credentials. However, write operations (INSERT, UPDATE, DELETE, CREATE TABLE) and private datasets require API keys with appropriate SQL permissions. The master API key grants comprehensive account access and should be protected carefully.

CARTO
carto-logo
  • Platform

  • Overview

  • Visualization

  • Analytics

  • App Development

  • Data Enrichment

  • Security & Governance

  • Gen AI

  • Pricing

  • Solutions

  • Data Analyst

  • Developer

  • Data Scientist

  • GIS Professional

  • By Industry

  • By Use Case

  • Resources

  • Customer Stories

  • Blog

  • Glossary

  • Documentation

  • Academy

  • Reports

  • Events

  • Webinars

  • Partners

  • Company

  • About us

  • Newsroom

  • Careers

  • Brand

  • Grants

Unlock the power of spatial analysis.

youtube logofacebook logotwitter logolinkedin logo

© CARTO 2024

Terms

Privacy Notice

Whistleblower Form