Authenticate with an API key

An Application programming interface key (API key) is a permanent access token that defines the scope and permission for granting a public-facing application access to ready-to-use services. With ArcGIS REST JS, you use the ApiKeyManager class to set your API key before accessing services.

In this tutorial, you create and configure an API key that enables access to the geocoding and routing services for an application.

Prerequisites

You need an ArcGIS account to create an API key and access location services.

Steps

Create an API key

You can create and configure your API key in the developer dashboard so that it is scoped to access the geocoding and routing services.

  1. Go to the API key page in your developer dashboard.

  2. On the left, click +New API Key and set the:

    • Title: GeocodingRoutingKey
    • Description API key configured to access the geocoding and routing services.
  3. Click Create API key

Configure the API key

If you have an ArcGIS Developer account, by default your key will have access to the free tier of location services. If you have an organization account, by default your key will be scoped to access basemaps and non-stored geocodes.

  1. In the Overview page of the API key, locate Location services at the bottom.

  2. In the Location services pane, click Configure services. Check the following service cards:

    • Geocoding (not stored)
    • Routing

    Un-check any other service cards.

  3. Click the Configure service(s) button.

  4. In the Overview page, click the copy icon to copy your API key to set later.

Create a new pen

  1. If you are using the CDN libraries, to get started.

Add references

The request package contains request/response processing, authentication helpers, and error handling.

  1. Add references to the request, geocoding, and routing packages.

    Use dark colors for code blocks
                                                                              
    Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
    
        const apiKey = "YOUR_API_KEY";
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
        async function app() {
          const places = await arcgisRest
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              outFields: ["PlaceName", "Place_addr", "Phone"],
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
          const end = [candidate.location.x, candidate.location.y];
          const directions = await arcgisRest
              stops: [
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        </script>
    
      </body>
    </html>

Set the API key

  1. Create an apiKey variable to set your key and an authentication variable that will call the fromKey method to create an instance of ApiKeyManager.

    Use dark colors for code blocks
                                                                              
    Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
    
        const apiKey = "YOUR_API_KEY";
    
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
        async function app() {
          const places = await arcgisRest
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              outFields: ["PlaceName", "Place_addr", "Phone"],
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
          const end = [candidate.location.x, candidate.location.y];
          const directions = await arcgisRest
              stops: [
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        </script>
    
      </body>
    </html>

Find places

Create a function to call the geocoding service to find McDonald's places around a location in Denver.

  1. Add a place variable to define a search string and a start variable to define the location to start searching from.

    Use dark colors for code blocks
                                                                              
    Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
        const apiKey = "YOUR_API_KEY";
    
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
    
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
    
        async function app() {
          const places = await arcgisRest
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              outFields: ["PlaceName", "Place_addr", "Phone"],
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
          const end = [candidate.location.x, candidate.location.y];
          const directions = await arcgisRest
              stops: [
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        </script>
    
      </body>
    </html>
  2. Create an async function called app. In the function, set the places variable to the geocode operation and define the parameters for address and location. Set the maxLocation to return only one result. Set outFields to return the PlaceName, Place_addr, and Phone of the restaurant. Lastly, set the authentication so the request uses your scoped API keys.

    Use dark colors for code blocks
                                                                              
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
        const apiKey = "YOUR_API_KEY";
    
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
    
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
    
        async function app() {
    
          const places = await arcgisRest
            .geocode({
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              },
              outFields: ["PlaceName", "Place_addr", "Phone"],
              authentication
            });
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
          const end = [candidate.location.x, candidate.location.y];
          const directions = await arcgisRest
              stops: [
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        </script>
    
      </body>
    </html>
  3. Display the results for the first result that is returned in the array of candidates in places. Call the JSON.stringify method to return all the properties of the candidate and insert 2 white space characters. This will be appended to the text content in the results HTML element.

    Use dark colors for code blocks
                                                                              
    Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
        const apiKey = "YOUR_API_KEY";
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
        async function app() {
          const places = await arcgisRest
            .geocode({
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              },
              outFields: ["PlaceName", "Place_addr", "Phone"],
              authentication
            });
    
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
          const end = [candidate.location.x, candidate.location.y];
          const directions = await arcgisRest
              stops: [
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        </script>
    
      </body>
    </html>
  4. Call the app function.

    Use dark colors for code blocks
                                                                              
    Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
        const apiKey = "YOUR_API_KEY";
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
        async function app() {
          const places = await arcgisRest
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              outFields: ["PlaceName", "Place_addr", "Phone"],
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
    
          const end = [candidate.location.x, candidate.location.y];
          const directions = await arcgisRest
              stops: [
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        }
    
        app();
        </script>
    
      </body>
    </html>
  5. In CodePen, run the application to view the address, placeName, extent, and other attributes.

Find the route

To generate a route, you need a minimum of a start and an end location. Use the start location (downtown Denver) and the first place result from the geocode operation to get the route and driving directions to the closest McDonald's.

  1. In the app function, define an end variable to the contain the coordinates of the first place (McDonald's) found from the call to the geocoding service.

    Use dark colors for code blocks
                                                                              
    Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
        const apiKey = "YOUR_API_KEY";
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
        async function app() {
          const places = await arcgisRest
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              outFields: ["PlaceName", "Place_addr", "Phone"],
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
    
          const end = [candidate.location.x, candidate.location.y];
    
          const directions = await arcgisRest
              stops: [
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        }
    
        app();
        </script>
    
      </body>
    </html>
  2. Call the solveRoute operation and define the stops with the start and end locations. Set the authentication so the request uses your scoped API key.

    Use dark colors for code blocks
                                                                              
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
        const apiKey = "YOUR_API_KEY";
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
        async function app() {
          const places = await arcgisRest
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              outFields: ["PlaceName", "Place_addr", "Phone"],
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
    
          const end = [candidate.location.x, candidate.location.y];
    
          const directions = await arcgisRest
            .solveRoute({
              stops: [
                start,
                end
              ],
              authentication
            });
    
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
    
        }
    
        app();
        </script>
    
      </body>
    </html>
  3. Display the path between the two locations set in directions by using the JSON.stringify method and appending the results in the results HTML element.

    Use dark colors for code blocks
                                                                              
    Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>ArcGIS REST JS</title>
        <style>
          body {
            font-family: monospace;
            color: white;
            overflow: auto;
            padding: 1rem;
          body,
            background: #000000;
        </style>
      </head>
    
      <body>
        <pre id="result"></pre>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script>
        <script src="https://unpkg.com/@esri/arcgis-rest-geocoding@4.0.0/dist/bundled/geocoding.umd.js"></script>
         <script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
        <script>
        const apiKey = "YOUR_API_KEY";
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
        async function app() {
          const places = await arcgisRest
              params: {
                address: place, // Place name to search for
                location: start,
                maxLocations: 1
              outFields: ["PlaceName", "Place_addr", "Phone"],
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
          const end = [candidate.location.x, candidate.location.y];
          const directions = await arcgisRest
            .solveRoute({
              stops: [
                start,
                end
              ],
              authentication
            });
    
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
        </script>
    
      </body>
    </html>
  4. In CodePen, run the application. You will see the JSON results for the path between the location in downtown Denver and the first McDonald's place that was found.

Result

The request will return the following :

What's next?

Learn how to use additional ArcGIS location services in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.