Tutorial: List API keys by expiration date

Learn how to list by expiration date using the ArcGIS API for Python.

List API keys

A list of API key credentials in a

API key credentials are used to authenticate requests to , , and . You can list API keys by expiration date to help you manage them.

This tutorial shows you how to list API keys in your by expiration date 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 API keys by expiration date

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

    main.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    from arcgis.gis import GIS
    from datetime import datetime, timezone
    
    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:
            app_item = gis.content.get(item.id)
            if app_item.apiToken1ExpirationDate != -1:
                ts = datetime.fromtimestamp(app_item.apiToken1ExpirationDate / 1000, timezone.utc).strftime('%Y-%m-%d %H:%M:%S')
                key_list.append((item.title, ts))
    
    sorted_keys = sorted(key_list, key=lambda x: x[1])
    
    
  5. Print the API keys sorted by expiration date.

    main.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    from arcgis.gis import GIS
    from datetime import datetime, timezone
    
    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:
            app_item = gis.content.get(item.id)
            if app_item.apiToken1ExpirationDate != -1:
                ts = datetime.fromtimestamp(app_item.apiToken1ExpirationDate / 1000, timezone.utc).strftime('%Y-%m-%d %H:%M:%S')
                key_list.append((item.title, ts))
    
    sorted_keys = sorted(key_list, key=lambda x: x[1])
    
    for name, expiration in sorted_keys:
        print(f"Name: {name}, Expiration: {expiration}")

View the results

After running the code, you will see a list of API keys in your portal sorted by expiration date.

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
Name: All Location Services API Key (test), Expiration: 2024-09-25 08:38:08
Name: API Key (basemaps only), Expiration: 2024-09-25 08:44:56
Name: Anita_UC_Demo_Key, Expiration: 2024-10-01 19:16:15
Name: API key for map tile layer, Expiration: 2024-10-07 20:23:48
Name: test api key credentials, Expiration: 2024-10-30 20:16:35
Name: All privileges api key, Expiration: 2024-10-30 20:51:29
Name: roads codepen (test), Expiration: 2024-11-04 20:48:21
Name: try to invalidate this key, Expiration: 2024-11-07 18:05:45
Name: For codepen for blog, Expiration: 2024-11-27 23:11:25
Name: Demo Key, Expiration: 2024-12-26 18:12:54
Name: Al's API key for testing, Expiration: 2024-12-31 14:34:06
Name: Parcels key, Expiration: 2025-01-06 15:30:33
Name: Parcel_Key_Demo_Layer, Expiration: 2025-01-08 00:20:40
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 items scoped to an API key

Use ArcGIS API for Python to list items scoped to an API key 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