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:

    • Basemaps
    • 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.

    Expand
    Use dark colors for code blocks
    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
      <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>
    
        </script>
    
    Expand

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.

    Expand
    Use dark colors for code blocks
    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
        <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);
    
    Expand

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.

    Expand
    Use dark colors for code blocks
    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
        const apiKey = "YOUR_API_KEY";
    
        const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
    
        const place = "McDonalds";
        const start = [-104.9903, 39.7392]; // Downtown Denver
    
    
    Expand
  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.

    Expand
    Use dark colors for code blocks
    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
        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
            });
    
    Expand
  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.

    Expand
    Use dark colors for code blocks
    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
          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);
    
    Expand
  4. Call the app function.

    Expand
    Use dark colors for code blocks
    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
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
    
        }
    
        app();
    
    Expand
  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.

    Expand
    Use dark colors for code blocks
    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
          const candidate = places.candidates[0];
          document.getElementById("result").textContent = JSON.stringify(candidate, null, 2);
    
          const end = [candidate.location.x, candidate.location.y];
    
        }
    
        app();
    
    Expand
  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.

    Expand
    Use dark colors for code blocks
    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
          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();
    
    Expand
  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.

    Expand
    Use dark colors for code blocks
    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
          const directions = await arcgisRest
            .solveRoute({
              stops: [
                start,
                end
              ],
              authentication
            });
    
          document.getElementById("result").textContent += JSON.stringify(directions, null, 2);
    
    Expand
  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

Below is the response from the service:

Use dark colors for code blocksCopy
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
[
  {
    "address": "McDonald's",
    "location": {
      "x": -104.98814299999998,
      "y": 39.741817000000026,
      "spatialReference": {
        "wkid": 4326,
        "latestWkid": 4326
      }
    },
    "score": 100,
    "attributes": {
      "PlaceName": "McDonald's",
      "Place_addr": "200 16th St, Denver, Colorado, 80202",
      "Phone": "(303) 534-6567"
    },
    "extent": {
      "xmin": -104.98914299999998,
      "ymin": 39.74081700000003,
      "xmax": -104.98714299999997,
      "ymax": 39.742817000000024,
      "spatialReference": {
        "wkid": 4326,
        "latestWkid": 4326
      }
    }
  },
  {
    "messages": [],

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.