To build applications with the JavaScript Maps SDK that access secure ArcGIS resources, you must implement API key authentication API key authentication is a type of authentication that uses an API key to authenticate requests to ArcGIS services and secure portal items. Learn more , OAuth 2.0 user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more , or app authentication App authentication is a type of authentication that grants a short-lived access token based on an OAuth 2.0 client ID and client secret, authorizing an application to access ArcGIS services and items. Learn more . After implementing an authentication method, your application can use the resulting access token to make requests to secure resources.

The type of authentication you use depends on the product you have and the type of application you are building.

Typical use caseAuthentication type
Public applications that do not require users to sign inAPI key authentication or App authentication
Private applications for members of an organization that require sign inUser authentication
Personal applications for your use onlyAPI key authentication, User authentication, App authentication

API key authentication

If you have ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise >11.4, you can use API key authentication to create a long-lived access token that authorizes secure service requests without requiring users to sign in. This method requires creating an API key credential API key credentials are an item that contains the parameters used to create and manage long-lived access tokens for API key authentication. They are a type of developer credential. Learn more and embedding the access token directly in your application, granting access to specific services and content based on the API key’s configured privileges.

API keys can be configured to access specific ArcGIS services and functionality, including location services, hosted data layers A hosted layer is an item in a portal that contains the properties and settings for a hosted data service or a layer in a hosted data service. Learn more , data services, and spatial analysis services. API keys can also be configured to access items in your portal and tasks in the portal itself. The configuration and assignment of privileges determine the overall capabilities and functionality of the API key.

API key authentication is well-suited for building public-facing applications A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. Learn more that do not require user sign-in.

Using API keys with the SDK

With the JavaScript Maps SDK, there are two ways to use API keys in your application:

  • Setting a global API key: This uses the esriConfig global variable or config module for all secure services and resources.
  • Setting an API key for a class or component: This uses the apiKey property available on specific classes and components that require an access token to access secure resources. This pattern is useful when you want to set different API keys for different services or resources in your application.

Steps

  1. Create an API key credential item in your portal.
  2. Assign privileges to define accessible services and data.
  3. Set the access token globally with esriConfig.apiKey or on a specific class.

Examples

Set an API key for global use

This example shows how to set an API key access token in your application at a global level. Implement this pattern when you want to use the same token for all secure services and resources in your application.

The access token associated with the API key credential needs to have the necessary privileges configured for all services and data that are used in the application. The esriConfig global variable must be defined first before adding other libraries.

Use the apiKey property to set an access token for all services and resources in your application.

<script>
var esriConfig = {
apiKey: "YOUR_ACCESS_TOKEN",
};
</script>
Go to tutorial

Set an API key for a specific class

This example shows how to set an access token for the BasemapStyle class. Implement this pattern when you want to set an access token for a specific class that requires it. The access token associated with the API key credential needs to have the necessary privileges configured for the specific service or resource that the class will access.

const [Basemap, BasemapStyle] = await $arcgis.import([
"@arcgis/core/Basemap.js",
"@arcgis/core/support/BasemapStyle.js",
]);
const basemap = new Basemap({
style: new BasemapStyle({
apiKey: "YOUR_ACCESS_TOKEN",
id: "arcgis/human-geography",
language: "es",
}),
});

Set API keys for global use and for a specific class

API key access tokens can be set globally and for specific classes in the same application. The following example shows how one access token can be set on the FeatureLayer class to access a private feature layer item, while another token can be set globally to access the Basemap Styles service.

<head>
<script>
// API key set globally used for requests to the Basemap Styles service.
var esriConfig = {
apiKey: "YOUR_ACCESS_TOKEN",
};
</script>
<script type="module" src="https://js.arcgis.com/5.0/"></script>
</head>
<body>
<arcgis-map basemap="arcgis/human-geography"></arcgis-map>
<script type="module">
const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");
// API key set for a specific class to access a secure feature layer item.
// This API key will take precedence over the global API key for requests made by the FeatureLayer.
const featureLayer = new FeatureLayer({
portalItem: {
id: "LAYER_ITEM_ID", // The access token must have privileges to access this item.
apiKey: "YOUR_ACCESS_TOKEN",
},
});
</script>
</body>

View example on GitHub

User authentication

If you have ArcGIS Online or ArcGIS Enterprise, you can also build applications that can authenticate users via their ArcGIS account. This process involves creating an OAuth 2.0 credential, which is used to generate a unique access token tied to the user’s privileges Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more . With this token, applications can access all services and resources available to the signed-in user, including location services, spatial analysis, hosted data, secure items, and portal operations such as administration. Access is determined by the user’s role and permissions.

User authentication is well-suited for building private applications A private application is an application that requires users to sign in with an ArcGIS account. It supports user authentication. Learn more for your organization that require users to sign in.

Steps

  1. Create an OAuth 2.0 credential in your portal to obtain a client ID, client secret, and redirect URL.
  2. Use these credentials in your application to initiate the OAuth 2.0 flow and request an access token with the IdentityManager.
  3. Use the access token to access services on behalf of the signed-in user.

Example

Implement user authentication

This example shows how to use the IdentityManager to handle user authentication using the OAuth 2.0 flow. The IdentityManager class provides methods to register OAuth 2.0 credentials, trigger the sign-in process, and manage user sessions. When a user successfully signs in, the IdentityManager generates an access token that can be used to authenticate requests to secure ArcGIS services and resources on behalf of the user.

const [OAuthInfo, identityManager] = await $arcgis.import([
"@arcgis/core/identity/OAuthInfo.js",
"@arcgis/core/identity/IdentityManager.js",
]);
var oAuthInfo = new OAuthInfo({
appId: "YOUR-CLIENT-ID",
});
identityManager.registerOAuthInfos([oAuthInfo]);
Go to tutorial

App authentication

If you have ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise, you can build applications using app authentication to generates a short-lived access token based on a set of OAuth 2.0 credentials. The access tokens are associated with your ArcGIS account, and can be used to authenticate requests to access secure ArcGIS services and items.

App authentication can be used to create public applications with a server-side component that access ArcGIS services and secure content items. It can also be used to create personal applications and scripts that perform spatial analysis and portal management operations.

To implement app authentication, OAuth 2.0 credentials (Client ID and Client Secret) are used to generate access tokens via the token endpoint of your portal service. These access tokens are then registered with the IdentityManager to authenticate requests to secure ArcGIS services and resources. App authentication is a secure way to allow applications to access protected resources without requiring user sign-in, as the access tokens can be scoped with specific privileges and have a limited lifespan.

Steps

  1. Create an OAuth 2.0 credential in your portal to obtain a client ID, client secret, and redirect URL.
  2. Include the client_id and client_secret from OAuth credentials OAuth credentials are an item that contains parameters required to implement user authentication or app authentication, including a client_id, client_secret, and redirect URIs. They are a type of developer credential. Learn more in your server script.
  3. Create an endpoint for clients to request access tokens.
  4. When a client requests a token, submit a request to the token endpoint An token endpoint is an endpoint of a portal service that can be queried to request an access token. It is used to implement user authentication OAuth2.0 flows. Learn more of your portal service A portal service provides the functionality to securely create, access, and manage content, data services, users, and groups in a portal. The service can be hosted by Esri or in ArcGIS Enterprise. Learn more .
  5. Deliver the resulting access token to the JavaScript Maps SDK client.
  6. The JavaScript Maps SDK client uses the access token to access secure resources A secure resource is any item or service in an ArcGIS that requires an ArcGIS account and authentication to access. Examples include ArcGIS Location Services, and items and data services in an ArcGIS portal. Learn more by registering the token with the IdentityManager.

Example

Implement app authentication

This example shows how to utilize app authentication in your application. In this example, a server-side script is used to generate an access token using the OAuth 2.0 credentials and the token endpoint of the portal service. The client-side application then requests an access token from the server, receives the token, and registers it with the IdentityManager to authenticate requests to secure resources.

const identityManager = await $arcgis.import("@arcgis/core/identity/IdentityManager.js");
// URL of the server endpoint that generates app authentication access tokens using OAuth credentials.
const appTokenURL = "http://localhost:3080/auth";
const tokenData = { nonce: 1234 };
// Send a request to the token server to exchange the app credentials for an access token.
const tokenRequest = await fetch(appTokenURL, {
method: "POST",
body: JSON.stringify(tokenData),
headers: {
"Content-Type": "application/json",
},
});
// Get the token response from the server and register it with the IdentityManager.
const tokenResponse = await tokenRequest.json();
// This method should be called again before the token expires.
identityManager.registerToken({
expires: tokenResponse.expires_in,
server: tokenResponse.appTokenBaseURL,
ssl: true,
token: tokenResponse.access_token,
userId: tokenResponse.arcgisUserId,
});

View example on GitHub

More resources