Package detail

@mapbox/mapbox-sdk

mapbox900.3kBSD-2-Clause0.16.1

JS SDK for accessing Mapbox APIs

mapbox, sdk, api, map

readme

@mapbox/mapbox-sdk

Build Status

A JS SDK for working with Mapbox APIs.

Works in Node, the browser, and React Native.

As of 6/11/18, the codebase has been rewritten and a new npm package released. The mapbox package is deprecated in favor of the new @mapbox/mapbox-sdk package. Please read the documentation and open issues with questions or problems.

Table of contents

Installation

npm install @mapbox/mapbox-sdk

If you are supporting older browsers, you will need a Promise polyfill. es6-promise is a good one, if you're uncertain.

The documentation below assumes you're using a JS module system. If you aren't, read "Pre-bundled files on unpkg.com".

Usage

There are 3 basic steps to getting an API response:

  1. Create a client.
  2. Create a request.
  3. Send the request.

Creating clients

To create a service client, import the service's factory function from '@mapbox/mapbox-sdk/services/{service}' and provide it with your access token.

The service client exposes methods that create requests.

const mbxStyles = require('@mapbox/mapbox-sdk/services/styles');
const stylesService = mbxStyles({ accessToken: MY_ACCESS_TOKEN });
// stylesService exposes listStyles(), createStyle(), getStyle(), etc.

You can also share one configuration between multiple services. To do that, initialize a base client and then pass that into service factory functions.

const mbxClient = require('@mapbox/mapbox-sdk');
const mbxStyles = require('@mapbox/mapbox-sdk/services/styles');
const mbxTilesets = require('@mapbox/mapbox-sdk/services/tilesets');

const baseClient = mbxClient({ accessToken: MY_ACCESS_TOKEN });
const stylesService = mbxStyles(baseClient);
const tilesetsService = mbxTilesets(baseClient);

Creating and sending requests

To create a request, invoke a method on a service client.

Once you've created a request, send the request with its send method. It will return a Promise that resolves with a MapiResponse.

const mbxClient = require('@mapbox/mapbox-sdk');
const mbxStyles = require('@mapbox/mapbox-sdk/services/styles');
const mbxTilesets = require('@mapbox/mapbox-sdk/services/tilesets');

const baseClient = mbxClient({ accessToken: MY_ACCESS_TOKEN });
const stylesService = mbxStyles(baseClient);
const tilesetsService = mbxTilesets(baseClient);

// Create a style.
stylesService.createStyle({..})
  .send()
  .then(response => {..}, error => {..});

// List tilesets.
tilesetsService.listTilesets()
  .send()
  .then(response => {..}, error => {..})

Overview of requests, responses, and errors

For more details, please read the full classes documentation.

MapiRequest

Service methods return MapiRequest objects.

Typically, you'll create a MapiRequest then send it. send returns a Promise that resolves with a MapiResponse or rejects with a MapiError.

MapiRequests also expose other properties and methods that you might use from time to time. For example:

  • MapiRequest#abort aborts the request.
  • MapiRequest#eachPage executes a callback for each page of a paginated API response.
  • MapiRequest.emitter exposes an event emitter that fires events like downloadProgress and uploadProgress.

For more details, please read the full MapiRequest documentation.

// Create a request and send it.
stylesService.createStyle({..})
  .send()
  .then(response => {..}, error => {..});

// Abort a request.
const req = tilesetsService.listTilesets();
req.send().then(response => {..}, error => {
  // Because the request is aborted, an error will be thrown that we can
  // catch and handle.
});
req.abort();

// Paginate through a response.
tilesetsService.listTilesets().eachPage((error, response, next) => {
  // Do something with the page, then call next() to send the request
  // for the next page.

  // You can check whether there will be a next page using
  // MapiResponse#hasNextPage, if you want to do something
  // different on the last page.
  if (!response.hasNextPage()) {..}
});

// Listen for uploadProgress events.
const req = stylesService.createStyleIcon({..});
req.on('uploadProgress', event => {
  // Do something with the progress event information.
});
req.send().then(response => {..}, error => {..});

MapiResponse

When you send a MapiRequest, the returned Promise resolves with a MapiResponse.

Typically, you'll use MapiResponse.body to access the parsed API response.

MapiResponses also expose other properties and methods. For example:

  • MapiResponse#hasNextPage indicates if there is another page of results.
  • If there is another page, MapiResponse#nextPage creates a MapiRequest that you can send to get that next page.
  • MapiResponse.headers exposes the parsed HTTP headers from the API response.

For more details, please read the full MapiResponse documentation.

// Read a response body.
stylesService.getStyle({..})
  .send()
  .then(resp => {
    const style = resp.body;
    // Do something with the style.
  }, err => {..});

// Get the next page of results.
tilesetsService.listTilesets()
  .send()
  .then(resp => {
    if (resp.hasNextPage()) {
      const nextPageReq = resp.nextPage();
      nextPageReq.send().then(..);
    }
  }, err => {..});

// Check the headers.
tilesetsService.listTilesets()
  .send()
  .then(resp => {
    console.log(resp.headers);
  }, err => {..});

MapiError

If the server responds to your MapiRequest with an error, or if you abort the request, the Promise returned by send will reject with a MapiError.

MapiErrors expose the information you'll need to handle and respond to the error. For example:

  • MapiError.type exposes the type of error, so you'll know if it was an HTTP error from the server or the request was aborted.
  • MapiError.statusCode exposes the status code of HTTP errors.
  • MapiError.body exposes the body of the HTTP response, parsed as JSON if possible.
  • MapiError.message tells you what went wrong.

For more details, please read the full MapiError documentation.

// Check the error.
stylesService.getStyle({..})
  .send()
  .then(response => {..}, error => {
    if (err.type === 'RequestAbortedError') {
      return;
    }
    console.error(error.message);
  });

Services

Please read the full documentation for services.

Pre-bundled files on unpkg.com

If you aren't using a JS module system, you can use a <script> tag referencing pre-bundled files on the CDN unpkg.com.

<script src="https://unpkg.com/@mapbox/mapbox-sdk/umd/mapbox-sdk.js"></script>
<script src="https://unpkg.com/@mapbox/mapbox-sdk/umd/mapbox-sdk.min.js"></script>

These files are a UMD build of the package, exposing a global mapboxSdk function that creates a client, initializes all the services, and attaches those services to the client. Here's how you might use it.

<script src="https://unpkg.com/@mapbox/mapbox-sdk/umd/mapbox-sdk.min.js"></script>
<script>
  var mapboxClient = mapboxSdk({ accessToken: MY_ACCESS_TOKEN });
  mapboxClient.styles.getStyle(..)
    .send()
    .then(..);
  mapboxClient.tilesets.listTilesets(..)
    .send()
    .then(..);
</script>

Development

Please read ./docs/development.md.

changelog

Changelog

0.16.1

  • Add: Add session_token as optional parameter in the geocoding services

0.16.0

  • Add: Add depart_at as optional parameter in the isochrone service

0.15.6

  • Fix: Add secondary_address to list of accepted types for Geocoding v6

0.15.5

  • Fix: Rename directions api annotation option max_speed to maxspeed #480

0.15.4

  • Fix: Set body = '' for POST, PUT, PATCH, and DELETE requests that do not have a body #479

0.15.3

  • Fix: Maximum Optimization V1 request size limits should not be enforced client-side

0.15.2

  • Add: Add depart_at and arrive_by as optional parameters in the directions service

0.15.1

  • Add: Add driving-traffic as a profile option in the isochrone service

0.15.0

  • Add: Geocoding v6 service

0.14.0

  • Add: Config for Tokens#createToken and Tokens#updateToken can now include the allowedApplications property.

0.13.7

  • Add: add additional EV values to the directions API request

0.13.6

  • Add: add additional valid values for the directions api annotations option
  • Chore: update documentation, budo, meow, and remark-preset-davidtheclark dependencies
  • Chore: update jest and associated dependencies

0.13.5

  • Revert: add driving-traffic profile to Isochrone service.
  • Fix: Pin CI node version to LTS release (current LTS is 16.x).
  • Fix: Fix-up package-lock.json with new metadata (based on node 14).
  • Fix: Update got dependency to 11.8.5 (Fix for CVE-2022-33987).
  • Add: add electric vehicle routing parameters to Directions service.

0.13.4

Add: add contours_meters Isochrone API parameter

0.13.3

Add: add ip as valid value for proximity parameter in the geocoding service

0.13.2

Add: add fuzzyMatch and worldview parameters to the geocoding service

0.13.1

Fix: Update got depdendency to 10.7.0

0.13.0

Add: add driving-traffic profile to Isochrone service.

0.12.1

0.12.0

  • Add: add bounding box parameter as a position option for Static#getStaticImage.
  • Add: add padding optional parameter for Static#getStaticImage.

0.11.0

  • Add: add fresh parameter to Styles#getStyle to bypass the cached version of the style.
  • Add: add routing parameter to Geocoding#forwardGeocode and Geocoding#reverseGeocoding.
  • Add: add driving-traffic profile to Optimization#getOptimization.
  • Add: add sortby parameter to Datasets#listDatasets.
  • Add: add Tilesets#updateTileset.
  • Add: add fallback, mapboxGLVersion and mapboxGLGeocoderVersion to Styles#getEmbeddableHtml.
  • Add: add pagination support to Tilesets#listTilesetJobs and Tilesets#listTilesetSources.
  • Breaking change: Uploads#createUpload's mapId parameter is now tileset, and tilesetName is now name to be consistent across the API. mapId and tilesetName are deprecated, but will still work and may be removed in a future release.
  • Add: add private option to Uploads#createUpload.
  • Fix: fixed an issue where array parameters containing falsy values (e.g. for the proximity parameter in forwardGeocode, where longitude or latitude coordinates are 0) were not being applied correctly.

0.10.0

  • Add: add new parameters to Tilesets#listTilesets: type, limit, sortBy, start and visibility.
  • Add: add Tilesets#tileJSONMetadata method to retrieve a Tileset TileJSON metadata.
  • Add: add new metadata parameter to Styles#getStyle to preserve mapbox: specific metadata from the style.
  • Add: add new Tilesets methods deleteTileset, createTilesetSource, getTilesetSource, listTilesetSources, deleteTilesetSource, createTileset, publishTileset, tilesetStatus, tilesetJob, listTilesetJobs, getTilesetsQueue, validateRecipe, getRecipe, updateRecipe.
  • Add: add new draft parameter to Styles#getStyle, Styles#deleteStyleIcon and Styles#getStyleSprite, Styles#getEmbeddableHtml to work with draft styles.
  • Fix: Fix responses containing binary data when using Static#getStaticImage, Styles#getStyleSprite and Styles#getFontGlyphRange.
  • Fix: Fix requests for highres sprites in Styles#getStyleSprite.
  • Fix: set position.bearing to 0 if position.pitch is defined and position.bearing is not in the Static API.
  • Fix: use tilesets.getRecipe in tilesets API example.

0.9.0

  • Add: add Isochrone API service.

0.8.0

  • Add: add new style parameters to the Static Images API service: addlayer, setfilter, and layer_id.
  • Breaking change: insertOverlayBeforeLayer is now before_layer in the Static Images API service. This change uses the API's name for the field and to support that the field can be used with the new style parameter addlayer.

0.7.1

  • Fix: add missing geometry key to Tilequery service.

0.7.0

  • Fix: filter empty waypoints from map matching requests.
  • Fix: fix url token placement for service with clients.

0.6.0

  • Fix: Tokens#updateToken can now set null value to referrers property to delete the property.
  • Fix: Tokens#updateToken can now set null value to resources property to delete the property.
  • Breaking change: change all references to referrer{s} to allowedUrl{s}.

0.5.0

  • Add: Config for Tokens#createToken and Tokens#updateToken can now include the referrers property.

0.4.1

  • Fix: Fix a CORS-related bug that caused Firefox to send preflight OPTIONS requests that were rejected by the server. This bug surfaced in Firefox with the Tilequery API, but may possibly have affected some other endpoints and some other browsers.

0.4.0

  • Breaking change & fix: Config for Static#getStaticImage now includes a position property that can be either "auto" or an object with coordinates, zoom, etc., properties. This fixes buggy behavior with the "auto" keyboard by forcing it to be mutually exclusive with all other positioning options.
  • Fix: Fix bug in Static#getStaticImage that resulted in reversed coordinates when creating a polyline overlay.
  • Fix: Loosen the type validation on coordinates, since longitudes greater than 180 or less than -180 are valid for some APIs.

0.3.0

  • Change: Rename MapMatching#getMatching to MapMatching#getMatch.
  • Change: Throw validation error if request configuration object includes invalid properties. This is a breaking change because it could cause your code to throw a new validation error informing you of a mistake in your code. But there is no change to the library's functionality: you'll just need to clean up invalid properties.

0.2.0

  • Add: Add Optimization API service.

0.1.3

  • Fix: Include all services in the UMD bundled client. Several were missing.

0.1.2

  • Chore: Use @mapbox/fusspot for validation; remove the local implementation.

0.1.1

  • Fix: Directions#getDirections and Geocoding#forwardGeocode stringify boolean query parameters like steps and autocomplete.

0.1.0

  • Brand new codebase. Please read the documentation and try it out! The mapbox npm package is now deprecated in favor of the new @mapbox/mapbox-sdk.