Learn how to authenticate a request to view users in the portal with an API key.
Prerequisites
An ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account.
Steps
Create a new app
-
Open a terminal and create a new folder for your project.
Use dark colors for code blocks Copy mkdir authenticate-with-an-api-key cd authenticate-with-an-api-key -
Initialize a new Node.js project. This creates a
package.jsonfile.Use dark colors for code blocks Copy npm init -
Install the required packages.
Use dark colors for code blocks Copy npm install @esri/arcgis-rest-request --save -
Create a new JavaScript file named
index.js.Use dark colors for code blocks Copy touch index.js
Get an access token
-
Sign in to your portal.
- ArcGIS Location Platform: Go to https://location.arcgis.com and sign in. In the dashboard, click My portal to go to your portal.
- ArcGIS Online: Go to https://arcgis.com and sign in.
-
In your portal, click Content > My content > New item.
-
Click Developer credentials > API key credentials and click Next.
-
In the Where will you use these credentials? menu, select Private application with selected privileges and access.
-
Click Next. Select No item access and click Next again.
-
In the Privileges window, select General privileges > Members > View and click Next.
-
Provide a title and description for your API key credential. Click Next.
-
In the Summary window, review the properties, privileges, and item access you have set. Then, click Next.
-
Select Generate the API key and go to item details page. I am ready to copy and save the key. Then, click Next.
-
Copy the access token from the window that appears and store it in a safe location.
Make a request
-
Open your
index.jsfile and import the library.index.jsUse dark colors for code blocks import { ApiKeyManager, request } from "@esri/arcgis-rest-request"; -
Paste in your access token.
index.jsUse dark colors for code blocks import { ApiKeyManager, request } from "@esri/arcgis-rest-request"; const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(accessToken); -
Search for users with the name
johnwithin the portal and print the results to the console.index.jsUse dark colors for code blocks import { ApiKeyManager, request } from "@esri/arcgis-rest-request"; const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(accessToken); const url = "https://www.arcgis.com/sharing/rest/community/users"; request(url, { authentication, params: { q: "john" // The query string to search the users against } }).then((response) => { console.log(JSON.stringify(response, null, 2)); }); -
Save the file, then run it from the terminal.
Use dark colors for code blocks Copy node index.js
You should now see the results printed in your console.
{
"total": 10000,
"start": 1,
"num": 10,
"nextStart": 11,
"results": [
{
"username": "JohnDoe",
"fullName": "John Doe",
"id": "123",
"firstName": "John",
"lastName": "Doe",
"provider": null,
"created": 1540585165000,
"modified": 1761576562000,
What's next?
Learn how to use additional location services in these tutorials: