Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@carto/api-client

CartoDB99.2kMIT0.5.13TypeScript support: included

Client library for CARTO APIs and framework-agnostic CARTO + deck.gl applications

readme

@carto/api-client

JavaScript (and TypeScript) client library for CARTO APIs and framework-agnostic CARTO + deck.gl applications.

Includes:

Installation

Install @carto/api-client:

npm install --save @carto/api-client

Documentation

Fetching data

Import vectorTableSource, vectorQuerySource, and other data source functions from the @carto/api-client package. These are drop-in replacements for the equivalent functions from the `@deck.gl/carto` package, and the same data source may be used with any number of layers or widgets. Tileset sources are not yet supported.

import { vectorTableSource } from '@carto/api-client';

const data = await vectorTableSource({
  accessToken: '••••',
  connectionName: 'carto_dw',
  tableName: 'carto-demo-data.demo_tables.retail_stores'
});

// → {name: string; value: number}[]
const categories = await data.widgetSource.getCategories({
  column: 'store_type',
  operation: 'count',
});

// → {value: number}
const formula = await data.widgetSource.getFormula({operation: 'count'});

// → {totalCount: number; rows: Record<string, number | string>[]}
const table = await data.widgetSource.getTable({
  columns: ['a', 'b', 'c'],
  sortBy: ['a'],
  rowsPerPage: 20
});

...

Column filter

To filter the widget source by a non-geospatial column, pass a filters property to the source factory function.

import {vectorTableSource} from '@carto/api-client';

const data = await vectorTableSource({
  accessToken: '••••',
  connectionName: 'carto_dw',
  tableName: 'carto-demo-data.demo_tables.retail_stores',
  filters: {
    store_type: {owner: 'widget-id', values: ['retail']},
  },
});

By default, filters affect all layers and widgets using a given data source. To exclude a particular widget from the filter, pass a filterOwner parameter matching the filters from which it should be excluded. In some cases, a widget's results should not be affected by a filter that the widget itself created.

// → {name: string; value: number}[]
const categories = await data.widgetSource.getCategories({
  filterOwner: 'widget-id',
  column: 'store_type',
  operation: 'count',
});

Spatial filter

To filter the widget source to a spatial region, pass a spatialFilter parameter (GeoJSON Polygon or MultiPolygon geometry) to any data fetching function.

// → {name: string; value: number}[]
const categories = await data.widgetSource.getCategories({
  column: 'store_type',
  operation: 'count',
  spatialFilter: {
    type: "Polygon"
    coordinates: [
      [
        [-74.0562, 40.8331],
        [-74.0562, 40.6933],
        [-73.8734, 40.6933],
        [-73.8734, 40.8331],
        [-74.0562, 40.8331]
      ]
    ],
  }
});

To create a spatial filter from the current deck.gl viewState:

import {WebMercatorViewport} from '@deck.gl/core';
import {createViewportSpatialFilter} from '@carto/api-client';

const viewport = new WebMercatorViewport(viewState);
const spatialFilter = createViewportSpatialFilter(viewport.getBounds());

Specifying columns to fetch

Factory functions, like vectorTableSource, support both layers and widgets. While reusing the same sources has advantages, including simplicity, it's important to understand which columns are fetched, which depends on the source type.

  • Table sources: Layers fetch only columns specified by the columns parameter. Widgets fetch only the columns they need, and are unaffected by the columns parameter.
  • Query sources: Source SQL query must include all columns needed by any layers or widgets using the source. Layers fetch only the subset specified by the columns parameter. Widgets fetch only the subset they need, and are unaffected by the columns parameter.
  • Tileset sources: Not yet supported.

Versioning

Package versioning follows Semantic Versioning 2.0.0.

License

Provided as open source under MIT License.

changelog

CHANGELOG

0.5

0.5.12

  • feat(sources): allow passing tags to baseSource and query methods (#193)

0.5.11

0.5.10

  • fix(widgets): orderBy support in WidgetRasterSource.getCategory (#210)

0.5.9

  • feat(widgets): orderBy support in WidgetSource.getCategory (#203)

0.5.8

  • feat(widgets): Add othersThreshold to WidgetSource.getCategories (#194)
  • perf(rasters): Improve performance of raster widget spatial filtering (#207)

0.5.7

  • feat(widgets): Allow local widget calculations without spatial filter (#204)
  • perf(widgets): Improve performance of local widget spatial filtering (#192, #201)

0.5.6

  • chore(widgets): Add AggregationTypes enum-like definition (#187)
  • feat(worker): Distribute worker script as standalone JS bundle (#189)
  • feat(widgets): Add widgetWorkerUrl option (#188)
  • feat(widgets): WidgetRemoteSource: custom aggregations for category, timeseries models (#174)

0.5.5

  • feat(fetchmap): Expose legend info from fetchMap (#179)
  • fix(widgets): Fix inference of 'spatialDataColumn' in some table and query widgets (#183)

0.5.4

  • fix(widgets): Fix support for 'storeGeometry' on tileset widgets (#181)
  • fix(widgets): Fix inference of 'spatialDataType' for dynamic point aggregation sources (#178)

0.5.3

  • chore(deps): Unpin h3-js dependency (#176)
  • chore(types): Export missing types used in public API (#159)

0.5.2

  • feat(filters): Export getApplicableFilters (#162)
  • fix(fetchmap): Bug fixes (#161, #163)
  • fix(widgets): Fix error computing min/max for large datasets (#164)
  • chore(build): Improve package compatibility (#158, #160)

0.5.1

  • chore(types): Cleanup for tilesets, rasters, and backward-compatibility (#150)
  • feat(fetchMap): Export legendSettings (#153)

0.5.0

  • BREAKING CHANGE: Replace 'abortController' with 'signal' parameter (#110)
  • feat: Add widget calculations for tileset sources (#50)
  • feat: Add widget calculations for raster sources (#119)
  • feat: Enable Web Workers for local tileset and raster widget calculations (#119)

0.4

0.4.10

  • feat: Add fetchMap() (#126, #132, #133, #135, #136, #137, #138, #139)
  • chore(widgets,deps): Clean up dataResolution references, examples, deps (#134)

0.4.9

  • feat: Remove spatialIndexReferenceViewState param (#128)

0.4.8

  • fix: Fix clientId defaults in query and source calls (#122)

0.4.7

  • fix: Fix clientId customization in table and query widget calls (#120)
  • feat: Add getDataFilterExtensionProps (#105, #113)
  • feat: Add option to override HTTP headers in widget calls (#100, #111)
  • feat: Add filters parameter in widget calls (#103)
  • chore: Update to turf.js v7.2 (#98)
  • chore: Enable compatibility with moduleResolution=nodenext (#106)

0.4.6

  • chore: Add repository and homepage in npm package metadata

0.4.5

  • types: Export TileResolution and SpatialFilterPolyfillMode types (#63)
  • fix: Propagate spatialDataColumn and tileResolution defaults to widget source (#64)

0.4.4

  • feat: Add support for spatial index types (H3, quadbin) in Widget APIs

0.4.3

  • feat: Add support foraggregationExp parameter to vectorQuerySource and vectorTableSource

0.4.2

  • fix: Fix incorrect column name lowercasing in Picking Model API

0.4.1

  • feat: Add cache control mechanism for sources and query APIs

0.4.0

  • feat: Add Picking Model API
  • refactor: Migrate sources from `@deck.gl/cartoto@carto/api-client`
  • deps: Remove `@deck.gl/carto` bundled dependency

0.3

0.3.0

0.2

0.2.0

  • feat: Initial release