Tutorials | what3words
We use cookies, including third party cookies, to improve your experience and for marketing purposes. By continuing to browse we assume you have consented to their use.Learn more
got it
All tutorials

NodeJS

intermediate

The Node.js API wrapper is useful for Node.js developers who wish to seamlessly integrate the what3words API into their Node.js applications, without the hassle of having to manage the low level API calls themselves.

The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///index.home.raft to latitude and longitude coordinates such as (-0.203586, 51.521251) and vice versa. It features a powerful autosuggest function, which can validate and autocorrect user input and limit it to certain geographic areas (this powers the search box on our map site). It allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request the list of all languages supported by what3words. For advanced users, autosuggest can be used to post-process voice output. See links on the left to navigate.

All coordinates are latitude,longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).

You will need a what3words API key to complete this tutorial.

2

Installation

To load the JavaScript wrapper for the what3words API, use NPM:

npm install --save @what3words/api axios
Copied
3

Setup

const api = require("@what3words/api");
            
api.setOptions({ key: "what3words-api-key" });
Copied
4

Usage

Convert to 3 word address

This function converts coordinates (expressed as latitude and longitude) to a 3 word address.

More information about convertTo3wa, including returned results is available in the what3words REST API documentation.

Find the words for (51.520847, -0.195521).:

api.convertTo3wa({lat:51.520847, lng:-0.195521})
  .then(data => console.log(data));
Copied

Convert to coordinates

This function converts a 3 word address to a position, expressed as coordinates of latitude and longitude.

It takes the words parameter as a string of 3 words 'table.book.chair'

More information about convertToCoordinates, including returned results is available in the what3words REST API documentation.

Find the words for ///filled.count.soap:

api.convertToCoordinates("filled.count.soap")
  .then(data => console.log(data));
Copied

AutoSuggest

When presented with a 3 words address which may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.

This method provides corrections for mis-typed words (including plural VS singular), and words being in the wrong order.

Optionally, clipping can narrow down the possibilities, and limit results to:

  • One or more countries
  • A geographic area (a circle, box or polygon)

This dramatically improves results, so we recommend that you use clipping if possible.

To improve results even further, set the focus to user’s current location. This will make autosuggest return results which are closer to the user.

More information about autosuggest, including returned results is available in the what3words REST API documentation.

Example: Basic Autosuggest call

api.autosuggest("fun.with.code")
  .then(data => console.log(data));
Copied

Example: AutoSuggest, adjust the number of results returned

api.autosuggest("fun.with.code", { nResults: 5 })
  .then(data => console.log(data));
Copied

Example: AutoSuggest, clipping the results returned to France and Germany

api.autosuggest("fun.with.code", { clipToCountry: ["FR", "DE"] })
  .then(data => console.log(data));
Copied

Example: AutoSuggest, clipping to a circle

api.autosuggest("fun.with.code", { clipToCircle: {center: {lat:51.4243877, lng:-0.34745}, radius:50} })
  .then(data => console.log(data));
Copied

Example: AutoSuggest, clipping to a polygon

api.autosuggest("fun.with.code", { clipToPolygon: [51.421,-0.343,52.6,2.3324,54.234,8.343,51.421,-0.343] })
  .then(data => console.log(data));
Copied

Example: AutoSuggest, clipping to a bounding box

api.autosuggest("fun.with.code", { clipToBoundingBox:{ southwest: { lat: 51.521, lng: -0.343 },
    northeast: { lat: 52.6, lng: 2.3324 }} })
  .then(data => console.log(data));
Copied

Example: AutoSuggest, Focus on (51.4243877,-0.34745)

api.autosuggest("fun.with.code", { focus: {lat:51.4243877, lng:-0.34745} })
  .then(data => console.log(data));
Copied

Example: AutoSuggest, Focus on (51.4243877,-0.34745) and then determine how many results within the response it applies to

api.autosuggest("fun.with.code", {focus: {lat:51.4243877, lng:-0.34745},nFocusResults: 2})
  .then(data => console.log(data));
Copied

Example: AutoSuggest, with Generic Voice input type

api.autosuggest("fun with code", {inputType: "generic-voice", language: "en"})
  .then(data => console.log(data));
Copied

Grid section

Grid section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.

The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned.

More information about gridSection, including returned results is available in the what3words REST API documentation.

Get a grid for (52.208867,0.117540) in the south west, and (52.207988,0.116126) in the north east:

api.gridSection({
    southwest: { lat: 52.208867, lng: 0.117540 },
    northeast: { lat: 52.207988, lng: 0.116126 }
  })
  .then(data => console.log(data));
Copied

Available languages

This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.

More information about availableLanguages, including returned results is available in the what3words REST API documentation.

api.availableLanguages()
  .then(data => console.log(data));
Copied

Handling errors

Errors returned from the API can be caught with the wrapper through the use of a catch function.

Within the catch function, code and message values which represent the error, are accessible from the error object parameter

api.convertToCoordinates("filled.count.soap")
  .then(function(response) {
    console.log("[convertToCoordinates]", response);
  })
  .catch(function(error) { // catch errors here
    console.log("[code]", error.code);
    console.log("[message]", error.message);
  });
Copied

Related tutorials

Back to top