Skip to content

Learn how to authenticate a request to view users in the portal with an API key.

Prerequisites

Steps

Create a new app

  1. Open a terminal and create a new folder for your project.

    Use dark colors for code blocksCopy
    1
    2
    mkdir authenticate-with-an-api-key
    cd authenticate-with-an-api-key

  2. Initialize a new Node.js project. This creates a package.json file.

    Use dark colors for code blocksCopy
    1
    npm init
  3. Install the required packages.

    Use dark colors for code blocksCopy
    1
    npm install @esri/arcgis-rest-request  --save

  4. Create a new JavaScript file named index.js.

    Use dark colors for code blocksCopy
    1
    touch index.js

Get an access token

  1. Sign in to your portal.

  2. In your portal, click Content > My content > New item.

  3. Click Developer credentials > API key credentials and click Next.

  4. In the Where will you use these credentials? menu, select Private application with selected privileges and access.

  5. Click Next. Select No item access and click Next again.

  6. In the Privileges window, select General privileges > Members > View and click Next.

  7. Provide a title and description for your API key credential. Click Next.

  8. In the Summary window, review the properties, privileges, and item access you have set. Then, click Next.

  9. Select Generate the API key and go to item details page. I am ready to copy and save the key. Then, click Next.

  10. Copy the access token from the window that appears and store it in a safe location.

Make a request

  1. Open your index.js file and import the library.

    index.js
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    import { ApiKeyManager, request } from "@esri/arcgis-rest-request";
    
    
  2. Paste in your access token.

    index.js
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    import { ApiKeyManager, request } from "@esri/arcgis-rest-request";
    
    const accessToken = "YOUR_ACCESS_TOKEN";
    const authentication = ApiKeyManager.fromKey(accessToken);
    
    
  3. Search for users with the name john within the portal and print the results to the console.

    index.js
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    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));
    });
  4. Save the file, then run it from the terminal.

    Use dark colors for code blocksCopy
    1
    node index.js

You should now see the results printed in your console.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
  "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,
Expand

What's next?

Learn how to use additional location services in these tutorials:

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