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.
Go to the API key page in your developer dashboard.
On the left, click +New API Key and set the:
Title: GeocodingRoutingKey
DescriptionAPI key configured to access the geocoding and routing services.
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.
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, to get started.
Add references
The request package contains request/response processing, authentication helpers, and error handling.
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.
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.
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.
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.