HOW TO

Retrieve the REST URL of ArcGIS Online hosted feature layers using ArcGIS API for Python

Last Published: December 21, 2022

Summary

The REST endpoint of an ArcGIS Online item contains useful information. However, manually obtaining the REST URL can be time-consuming if there are many items. Using ArcGIS API for Python, information such as the REST URL for each item can be generated in an Excel file. This article provides the workflow to retrieve the REST URL and other details of the ArcGIS Online items using ArcGIS API for Python.

Procedure

  1. Import the necessary modules.
from arcgis.gis import GIS
import datetime
import os, re, csv
import pandas as pd
  1. Specify the ArcGIS Online credentials to use.
gis = GIS('https://www.arcgis.com', 'USERNAME', 'PASSWORD')
  1. Search for the relevant items.
search_result = gis.content.search(query = 'owner:OWNER_NAME', item_type="Feature Service", max_items=NUMBER OF ITEMS)
#(search_result)
# list to record items
  1. Create a list parameter.
list_of_items = list()
list_of_items.append(['title', 'id', 'views', 'created', 'owner', 'size', 'access', 'url'])
  1. Store the searched items information in the created list.
for i in search_result:

  try:
 
      if hasattr(i, 'url'):
        #source = i.sourceUrl
        unix_date =i.created # unix ms second date/time
        normal_date = datetime.datetime.fromtimestamp(unix_date / 1e3)

        list_of_items.append([i.title, i.itemid, i.numViews, normal_date, i.owner, i.size, i.access, i.url])
        
  except:
   continue
  1. Convert the list parameter to a Pandas DataFrame and save the DataFrame as an Excel file in a specified folder location.
df = pd.DataFrame(list_of_items)
df.to_excel(r'<FOLDER LOCATION>\<FILE NAME>.xlsx')
print('Item details are saved in the excel file.')

The code block below shows the full script for the 20 searched hosted features owned by TestUser.

from arcgis.gis import GIS
import datetime
import os, re, csv
import pandas as pd

gis = GIS('https://www.arcgis.com', 'USERNAME123', 'PASSWORD123')

search_result = gis.content.search(query = 'owner:TestUser', item_type='Feature Service', max_items=20)

list_of_items = list()
list_of_items.append(['title', 'id', 'views', 'created', 'owner', 'size', 'access', 'url'])

for i in search_result:

  try:
 
      if hasattr(i, 'url'):
        unix_date =i.created # unix ms second date/time
        normal_date = datetime.datetime.fromtimestamp(unix_date / 1e3)

        list_of_items.append([i.title, i.itemid, i.numViews, normal_date, i.owner, i.size, i.access, i.url])
        
  except:
   continue

df = pd.DataFrame(list_of_items)
df.to_excel(r'C:\Users\USER\Desktop\Download Folder\item details.xlsx')
print('Item details are saved in the excel file.')

Article ID: 000028940

Software:
  • ArcGIS Online
  • ArcGIS API for Python 1 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options