Skip to content

ApiKeyManager

Class

Used to authenticate methods in ArcGIS REST JS with an API keys. The instance of ApiKeyManager can be passed to IRequestOptions.authentication to authenticate requests.

Use dark colors for code blocksCopy
1
2
3
import { ApiKeyManager } from '@esri/arcgis-rest-request';

const apiKey = new ApiKeyManager.fromKey("...");

In most cases however the API key can be passed directly to the IRequestOptions.authentication.

Implements

Inheritance: ApiKeyManagerAuthenticationManagerBase

Constructors

constructor

Class Constructor
new ApiKeyManager(optionsIApiKeyOptions): ApiKeyManager
Parameters
ParameterType
optionsIApiKeyOptions
Returns 
ApiKeyManager

Properties

PropertyTypeNotes
portalstring

The current portal the user is authenticated with.

portal

Class Property
portal: string = "https://www.arcgis.com/sharing/rest"

The current portal the user is authenticated with.

Accessors

AccessorReturnsNotes
get token()string

Gets the current access token (the API Key).

get username()string

The username of the currently authenticated user.

token

Class Accessor
get token(): string

Gets the current access token (the API Key).

Returns 
string

username

Class Accessor
get username(): string

The username of the currently authenticated user.

Returns 
string

Methods

Hide inherited methods
MethodReturnsNotes
void

Clear the cached user infornation. Usefull to ensure that the most recent user information from AuthenticationManagerBase.getUser is used.

getToken(url)Promise<string>

Gets the current access token (the API Key).

getUser(requestOptions?)
inherited
Promise<IUser>

Returns information about the currently logged in user. Subsequent calls will not result in additional web traffic.

getUsername()
inherited
Promise<string>

Returns the username for the currently logged in user. Subsequent calls will not result in additional web traffic. This is also used internally when a username is required for some requests but is not present in the options.

serialize()string

Serializes the ApiKeyManager instance to a JSON string.

toJSON(){ portal: string; token: string; type: string; username: string }

Converts the ApiKeyManager instance to a JSON object. This is called when the instance is serialized to JSON with JSON.stringify().

deserialize(serialized)ApiKeyManager

Deserializes a JSON string previously created with ApiKeyManager.deserialize to an ApiKeyManager instance.

fromKey(apiKey)ApiKeyManager

The preferred method for creating an instance of ApiKeyManager.

clearCachedUserInfo

inherited
Class Method
clearCachedUserInfo(): void

Clear the cached user infornation. Usefull to ensure that the most recent user information from AuthenticationManagerBase.getUser is used.

Returns 
void
Inherited from AuthenticationManagerBase.clearCachedUserInfo

getToken

Class Method
getToken(urlstring): Promise<string>

Gets the current access token (the API Key).

Parameters
ParameterType
urlstring
Returns 
Promise<string>

getUser

inherited
Class Method
getUser(requestOptions?IRequestOptions): Promise<IUser>

Returns information about the currently logged in user. Subsequent calls will not result in additional web traffic.

Use dark colors for code blocksCopy
1
2
3
4
manager.getUser()
  .then(response => {
    console.log(response.role); // "org_admin"
  })
Parameters
ParameterTypeNotes
requestOptionsIRequestOptions

Options for the request. NOTE: rawResponse is not supported by this operation.

Returns 
Promise<IUser>

A Promise that will resolve with the data from the response.

Inherited from AuthenticationManagerBase.getUser

getUsername

inherited
Class Method
getUsername(): Promise<string>

Returns the username for the currently logged in user. Subsequent calls will not result in additional web traffic. This is also used internally when a username is required for some requests but is not present in the options.

Use dark colors for code blocksCopy
1
2
3
4
manager.getUsername()
  .then(response => {
    console.log(response); // "casey_jones"
  })
Returns 
Promise<string>
Inherited from AuthenticationManagerBase.getUsername

serialize

Class Method
serialize(): string

Serializes the ApiKeyManager instance to a JSON string.

Use dark colors for code blocksCopy
1
2
3
4
5
import { ApiKeyManager } from '@esri/arcgis-rest-request';

const apiKey = new ApiKeyManager.fromKey("...")

localStorage.setItem("apiKey", apiKey.serialize());
Returns 
string

The serialized JSON string.

toJSON

Class Method
toJSON(): { portal: string; token: string; type: string; username: string }

Converts the ApiKeyManager instance to a JSON object. This is called when the instance is serialized to JSON with JSON.stringify().

Use dark colors for code blocksCopy
1
2
3
4
5
import { ApiKeyManager } from '@esri/arcgis-rest-request';

const apiKey = new ApiKeyManager.fromKey("...")

const json = JSON.stringify(session);
Returns 
{ portal: string; token: string; type: string; username: string }

A plain object representation of the instance.

    deserialize

    static
    Class Method
    deserialize(serializedstring): ApiKeyManager

    Deserializes a JSON string previously created with ApiKeyManager.deserialize to an ApiKeyManager instance.

    Use dark colors for code blocksCopy
    1
    2
    3
    import { ApiKeyManager } from '@esri/arcgis-rest-request';
    
    const apiKey = ApiKeyManager.deserialize(localStorage.getItem("apiKey"));
    Parameters
    ParameterTypeNotes
    serializedstring

    The serialized JSON string.

    Returns 
    ApiKeyManager

    The deserialized ApiKeyManager instance.

    fromKey

    static
    Class Method
    fromKey(apiKeystring | IApiKeyOptions): ApiKeyManager

    The preferred method for creating an instance of ApiKeyManager.

    Parameters
    ParameterType
    apiKeystring | IApiKeyOptions
    Returns 
    ApiKeyManager

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