Get Started with Node.js

Make sure you have polyfills for fetch and FormData installed before using any ArcGIS REST JS library. You can find npm install commands for all packages in the API reference.

npm install @esri/arcgis-rest-request @esri/arcgis-rest-auth cross-fetch isomorphic-form-data

Require cross-fetch and isomorphic-form-data before using any of the ArcGIS REST JS methods.

// ensures fetch is available as a global
require("cross-fetch/polyfill");
require("isomorphic-form-data");

const { request } = require("@esri/arcgis-rest-request");

request("https://www.arcgis.com/sharing/rest/info")
  .then(response => console.log(response));

Or, if using NodeJS version v13 or higher, if you set "type": "module" in the package.json you can use ES Modules import syntax:

import fetch from "node-fetch";
import FormData from "isomorphic-form-data";
import arcgisRestRequest from "@esri/arcgis-rest-request";
arcgisRestRequest.setDefaultRequestOptions({ fetch, FormData });

arcgisRestRequest.request("https://www.arcgis.com/sharing/rest/info")
  .then(response => console.log(response));

You can also pass through your own named fetch implementation.

const fetch = require("node-fetch")
const {
  request,
  setDefaultRequestOptions
} = require("@esri/arcgis-rest-request");

// one by one
request("https://www.arcgis.com/sharing/rest/info", { fetch })

// or in *every* request
setDefaultRequestOptions({ fetch })

Demo - Express

Authentication

To access premium content and services without asking for user credentials, using an API key or application credentials is typically the best approach.

API Key

// no auth required
request(`https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve?token={API_KEY}`)

Application credentials

const { ApplicationSession } = require("@esri/arcgis-rest-auth");

const authentication = new ApplicationSession({
  clientId: "public",
  clientSecret: "secret"
})

// url not accessible to anonymous users
const url = `https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World`

// token will be appended by rest-js
request(url, {
  authentication
})

Demo - batch geocoding

Applications cannot create, share, access or modify items in ArcGIS Online or ArcGIS Enterprise. For this, a UserSession is more appropriate.

const { UserSession } = require("@esri/arcgis-rest-auth");

// hardcoded username / password
const authentication = new UserSession({
  username: "jsmith",
  password: "123456"
})

See the Browser Authentication for more information about implementing OAuth 2.0.