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.
To learn more about API keys and access tokens, visit API keys in the Mapping APIs and location services guide. For a more in-depth tutorial on creating and managing API keys (including referrers), visit the Create and manage an API key tutorial.
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.
Go to the API key page in your developer dashboard.
On the left , click +New API Key and set the:
Title : GeocodingRoutingKey
Description API key configured to access the geocoding and routing services.
Click Create 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.
In the Overview page of the API key, locate Location services at the bottom.
In the Location services pane, click Configure services . Check the following service cards:
Geocoding (not stored) Routing Un-check any other service cards.
Click the Configure service(s) button.
In the Overview page, click the copy icon to copy your API key to set later.
Create a new pen If you are using the CDN libraries, use this pen to get started. Add references The request
package contains request/response processing, authentication helpers, and error handling.
Add references to the request
, geocoding
, and routing
packages.
Use dark colors for code blocks
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
Set the API key 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
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
Find places Create a function to call the geocoding service to find McDonald's places around a location in Denver.
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
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
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
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
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
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
Call the app
function.
Use dark colors for code blocks
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
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.
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
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
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
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
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
Show more lines
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;
}
pre {
overflow : auto;
padding : 1rem ;
}
body ,
pre {
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
.solveRoute({
stops : [
start,
end
],
authentication
});
document .getElementById( "result" ).textContent += JSON .stringify(directions, null , 2 );
}
app();
</ script >
</ body >
</ html >
Show more lines
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 results :
What's next? Learn how to use additional ArcGIS location services in these tutorials: