About this demo
You will build a Node.js script, app-auth.mjs, that authenticates as an application, without a user, with OAuth 2.0 credentials and the client credentials flow:
- It exchanges a client ID and client secret for an app token with
ApplicationCredentialsManagerfrom ArcGIS REST JS. - It calls the portals/self endpoint with that token and prints the privileges the token carries.
You will create the OAuth credentials in the portal first, then write the script that uses them.
Check prerequisites
You will need an ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account that can create developer credentials. In ArcGIS Online and ArcGIS Enterprise, that means a user type of Creator or higher.
To run the script: Node.js 18 or later.
Create the OAuth credentials
Create the credentials for your script in your portal:
- Go to Content > My content > New item.
- Click Developer credentials.
- Select OAuth 2.0 credentials (For app authentication).
- Select Private application with selected privileges, so the token gets only the privileges you grant.
- Select No item access. This script reads no items; for your own app, grant access only to the items it needs.
- Leave the privileges off. This script needs none; for your own app, enable only the ones it calls.
- Leave the referrer URLs empty. A backend script doesn’t need them: they help only when the token ends up in a web browser.
- Fill in the item details.
- Review the summary and create the item.
- Copy the Client ID and the Client Secret.










Install the package
@esri/arcgis-rest-request signs you in and sends the requests. Run npm install in the project folder.
Import the functions
The script uses:
ApplicationCredentialsManager: to sign in with app credentialsrequest: to call the REST API with the token
Configure the script
Paste the client ID and the client secret from the credentials item:
The client secret is as sensitive as a password. Keep it in server-side code only, and out of your repository: in production, read it from the environment or a secret manager.
Authenticating against ArcGIS Enterprise?
If you created the credentials in ArcGIS Enterprise, set the portal host and web adaptor, without https://:
Sign in as the application
ApplicationCredentialsManager.fromCredentials takes the client ID, the client secret and the portal’s REST URL: the portal URL followed by /sharing/rest. It sends no request yet.
Request a token
getToken sends grant_type=client_credentials, client_id and client_secret to the portal’s oauth2/token endpoint, and stores the token and its expiration in token and expires.
The token lasts two hours by default. Later calls to getToken return the stored token until it expires, then request a new one with the client secret: a long-running job needs no refresh logic of its own.
Make a request
Pass authentication to request, or to any other REST JS function: it calls getToken and adds the token to the request.
The script calls portals/self, which returns the application’s title and the privileges of its token, among other details.
Run the script
Run node app-auth.mjs (or npm start). It prints the token, its expiration, the title of the credentials item and the privileges of the token.
Your script authenticates as an application. To act on behalf of your users instead, sign them in with OAuth 2.0 in a browser app.