Build a public application
Create a basic web application in CARTO compatible with any Javascript Framework
Last updated
Create a basic web application in CARTO compatible with any Javascript Framework
Last updated
Here you will learn the basic concepts required to create a public web application using CARTO, compatible with any Javascript Development Framework. With CARTO you don't need to be a geospatial expert to develop a geospatial application, so if you're a web developer you shouldn't have any issues following this guide.
After completing this guide you will be familiar with the following concepts:
Scaffolding your application.
Adding a basemap
Creating an API Access Token with limited access to your data warehouse.
Visualizing a dataset with deck.gl
Publishing your app
During this guide, we're using the CARTO Data Warehouse. The process explained here is also compatible with other Warehouses like BigQuery, Snowflake, Redshift, or Postgres. Instead of using connection=carto_dw
, you need to use connection=<your_connection>.
We will use Vite to manage the tooling of our frontend application.
With NPM:
Then you can choose your favorite framework (Vanilla, Vue, React, Preact, etc..) and Typescript or Javascript. In order to make this guide the most compatible as possible, we're going to use Vanilla and for the variant, we're going to select Typescript.
After that, you should have a project up and running at http://127.0.0.1:5174/
Open the project you've just created in your favorite Code Editor (we recommend Visual Studio Code), and let's do some cleanup in the default template provided by Vite in order to have a full empty space.
Remove src/counter.ts file.
Replace src/main.ts with the following:
Replace src/style.css with the following:
You will now see a completely empty page. No worries! We're going to start adding content in the following sections.
We're going to include a basemap in your application. As explained in the Basemap section, you can use multiple basemaps (CARTO Basemaps, Google Maps, Amazon Location, etc...). For this guide we'll use the CARTO Basemaps, as they're available to all CARTO organizations, and do not require a third-party service.
First, we're going to install the required dependencies:
Then, create a file src/map.ts with this content:
Next, add this to src/style.css:
And replace src/main.ts with this code:
In order to use the CARTO APIs you need to authenticate your requests. For a public application the recommended approach is do so by creating an API Access Token. This token will be public so you need to guarantee it has limited access to the resources of your public application. You can read more about API Access Tokens here.
In this application, we're going to visualize the table: carto-demo-data.demo_tables.populated_places.
To create an API Access Token with access to the previous table, you need to go to CARTO Workspace -> Developers -> Credentials -> API Access Tokens. More info on these steps here.
You should add the previous table as a grant:
We also recommend limiting the token by referers URLs.
The process is exactly the same if you're using another warehouse like BigQuery, Snowflake, Redshift, or PostgreSQL.
This API Access Token is an environment configuration, so let's add it as an environment variable.
Create a file .env in the root folder:
If you have a different region, you need to modify VITE_API_BASE_URL.
Now, we're going to visualize the populated places dataset using deck.gl and CARTO Maps API. In this section, we're going to modify src/map.ts.
Import the requirements from CARTO for deck.gl:
Set the credentials to connect with CARTO:
Finally, create the source and layer to visualize both the populated places dataset and pass it to the Deck class:
Congratulations! You now have a working public application that will display your dataset in a map for any viewer. The application is a static website that can be deployed on your favorite web server.
Using the following command you can generate the necessary static files:
Vite also provides a guide to deploy a static website on different platforms like GitHub Pages, GitLab Pages, Netlify, Vercel, Firebase, etc.
All the code for this guide is available on GitHub.
We recommend you to visit Visualization with deck.gl to learn more about the different visualizations you can create using deck.gl. There are many examples in our gallery that might be useful to improve your application!