Learn how to list items scoped to an API key credentials using the ArcGIS API for Python.
Scoping items in ArcGIS portal to an API key enables control over data access and editing permissions. You can restrict access to specific items such as hosted feature layers or app builders 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 ArcGIS Location Platform, ArcGIS Online or ArcGIS Enterprise account for this tutorial.
Steps
Set up your development environment
- Install the ArcGIS API for Python.
- Start your Jupyter Notebook environment locally.
List items scoped to an API key
-
Add the following
import
statements to your code.main.pyfrom arcgis.gis import GIS
-
Establish a connection to the portal using the GIS() module.
main.pyfrom arcgis.gis import GIS gis = GIS() ## Default portal: "https://arcgis.com/"
-
Call the
search
method using theitem
filter to return all API keys._type=" Application" main.pyfrom arcgis.gis import GIS gis = GIS() ## Default portal: "https://arcgis.com/" items = gis.content.search(query="", item_type="Application", max_items=-1)
-
Use a
for
loop to get the API keys.main.pyfrom 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))
-
Use the
app
property to access the scoped items for an API key._info["privileges"] main.pyfrom 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.
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
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.