fetchMap
fetchMap is a function designed to allow developers to retrieve layers and other details from a CARTO Builder map, in order to integrate them into custom applications. Developers can then choose to:
Replicate the map nearly exactly as configured in CARTO
Integrate those layers in the application with customizations
Use this information in any other way (eg: storing map metadata in a database)
Usage
import {Deck} from '@deck.gl/core';
import {fetchMap} from '@carto/api-client';
const cartoMapId = 'ff6ac53f-741a-49fb-b615-d040bc5a96b8';
const mapData = await fetchMap({cartoMapId, credentials, autoRefresh, onNewData});Options
type FetchMapOptions = {
apiBaseUrl?: string;
cartoMapId: string;
accessToken?: string;
autoRefresh?: number;
onNewData?: (map: any) => void;
clientId?: string;
headers?: Record<string, string>;
};cartoMapId: The map ID from CARTO Builder that you want to retrieve using fetchMap. Learn how to obtain the ID from your maps here.
accessToken (optional): When the map that you're requesting is private/shared (in other words, not public) you will be required to pass a valid access token. You can use either:
OAuth Access Token: A token that represents a user with access to the map. Learn more about OAuth Access Tokens.
Map API Access Token: A long-lived, read-only token automatically generated for each map, which can be obtained from the map's sharing settings. This is the simplest method for accessing private maps.
apiBaseUrl (optional): The base URL that hosts your APIs. It varies depending on your CARTO region/deployment method. By default it will use
https://gcp-us-east1.api.carto.comwhich is the URL for the CARTO cloud US-based tenant, but it will be different if you operate in another region or in your own deployment. Learn how to obtain your API Base URL.autoRefresh (optional): Interval in seconds at which to autoRefresh the data from the map. If provided,
onNewDatamust also be provided. Make sure your map's data freshness settings are consistent with the desired experience.onNewData (optional): Callback function that will be invoked whenever data in layers is changed. If provided,
autoRefreshmust also be provided.clientId (optional): An arbitrary string that can be used to identify the application, area or functionality that is performing the requests. It will be reflected later in the CARTO Activity Data, allowing developers to track usage in their applications.
headers (optional): An array of valid HTTP headers that will be attached to all requests originating from this source. This is useful to send any extra header or to control the cache in your sources.
Response
The response of fetchMap will be a long, structured JSON that contains all the necessary information in order for you to integrate that map in your own CARTO + deck.gl application.
We recommend developers to use Typescript, following the types available in @carto/api-client to build code that depends on fetchMap.
Recommended approach: import fetchMap from @carto/api-client
When importing from @carto/api-client, the
layersarray will contain properties for CARTO + deck.gl layers, not the layers themselves. In order to build the layers in deck.gl you can use your own logic, or use ourLayerFactoryimplementation, available in this example.
Deprecation path: importing fetchMap from @deckgl/carto ,
When importing from @deck.gl/carto, the layers will be automatically compatible with CARTO + deck.gl, following the previous implementation of fetchMap. This may be removed in future major versions of deck.gl
Basic examples
Static display — prototyping with no changes
You can use the response from fetchMap directly in a deck.gl map, to achieve a static display of the map layers exactly as configured in Builder.
Accessing map metadata
You can easily inspect a particular part of the map metadata (such as styling properties) by accessing the fetchMap response object, following the reference above. For example, we can easily count the number of layers that have been configured in the map, and show the title of the map.
Modifying layer styles
Most developers will want to integrate the layers from fetchMap into their own deck.gl application, harmonizing the properties with the ones used in the application.
Below is an example demonstrating how to initialize the layers, override the visibility property of one layer, adjust the getRadius property of another, and leave a third layer unchanged:
Modifying interactions, tooltips, and popups
Using the popupSettings from the fetchMap response we can understand how the interactions were configured in the original map, and use that information to create our own interactions, having full control of the final UX.
Re-building legends from your map
Using the scales property available in each layer of the fetchMap response, we can easily build a legend that matches very closely the legend found in CARTO Builder, or create our own completely custom legend for our application. Here's a basic example that accesses those properties:
Complete example
A fully working example of a simple application using fetchMap can be found in our Examples gallery.
Last updated
Was this helpful?
