Tutorial: List items scoped to an API key

Learn how to list scoped to an using the ArcGIS API for Python.

List API keys

A list of items you can choose to be scoped with an existing API key credentials in a

Scoping items in ArcGIS to an API key enables control over data access and editing permissions. You can restrict access to specific items such as or such as ArcGIS Instant Apps or ArcGIS Dashboards. This approach ensures apps or users with the API key can have authorized access and perform actions.

This tutorial shows you how to list items scoped to an API key using the arcgis.gis module in the ArcGIS API for Python.

Prerequisites

You need an , or account for this tutorial.

Steps

Set up your development environment

  1. Install the ArcGIS API for Python.
  2. Start your Jupyter Notebook environment locally.

List items scoped to an API key

  1. Add the following import statements to your code.

    main.py
    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
    
    from arcgis.gis import GIS
    
    
  2. Establish a connection to the portal using the GIS() module.

    main.py
    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
    
    from arcgis.gis import GIS
    
    gis = GIS() ## Default portal: "https://arcgis.com/"
    
    
  3. Call the search method using the item_type="Application" filter to return all API keys.

    main.py
    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
    
    from arcgis.gis import GIS
    
    gis = GIS() ## Default portal: "https://arcgis.com/"
    
    items = gis.content.search(query="", item_type="Application", max_items=-1)
    
    
  4. Use a for loop to get the API keys.

    main.py
    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
    
    from arcgis.gis import GIS
    
    gis = GIS() ## Default portal: "https://arcgis.com/"
    
    items = gis.content.search(query="", item_type="Application", max_items=-1)
    
    key_list = []
    for item in items:
        if "APIToken" in item.typeKeywords:
            key_list.append(gis.content.get(item.id))
    
    
  5. Use the app_info["privileges"] property to access the scoped items for an API key.

    main.py
    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
    
    from arcgis.gis import GIS
    
    gis = GIS() ## Default portal: "https://arcgis.com/"
    
    items = gis.content.search(query="", item_type="Application", max_items=-1)
    
    key_list = []
    for item in items:
        if "APIToken" in item.typeKeywords:
            key_list.append(gis.content.get(item.id))
    
    for key in key_list:
        print("API key name: " + key.title)
        print("API key ID: " + key.id)
        if "privileges" in key.app_info:
            for scoped_item in key.app_info["privileges"]:
                if "portal:app:access:item" in scoped_item:
                    itm = gis.content.get(scoped_item.split(":")[-1])
                    if itm != None:
                        print("  Scoped Item Name: " + itm.title)
                        print("  Scoped Item ID: " + itm.id)
                        print("")
                    else:
                        print("  ID: " + scoped_item.split(":")[-1] + " does not exist")
        print("---")

View the results

After running the code, you will see a list of items scoped to API keys in your portal.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
API key name: Parcels_Feature_Layer_View
API key ID: e6a39ab6dc064ba785bcc32dad3fc102
  Scoped Item Name: Parcel_Demo_Layer_View
  Scoped Item ID: 6a77f0841a1043b68ca8aac6052a6de2

---
API key name: yeyeye API key
API key ID: 1d1b471107f7471d842ca8d832000e29
  Scoped Item Name: My offline map private
  Scoped Item ID: a85c10a7e00946aeb45b77c6a11947f8

---
API key name: Feedback Service API Key
Expand

What's next

Copy a hosted layer item

Use the portal service to copy a hosted layer item in your portal.


Set sharing level for an item

Use the portal service to set the sharing level for an item in your portal.


Create a new group and add members

Use the portal service to create a new group and add members in your portal.


List API keys by expiration date

Use ArcGIS API for Python to list API keys in your portal.


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

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close