Learn how to access a hosted feature layer and get its properties.
Prerequisites
An ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account.
Steps
Create a feature layer
For this tutorial, you will use the Santa Monica Parcels dataset to create a private hosted feature layer in your portal.
-
In your web browser, go to the Santa Monica Parcels item.
-
Click the Download button to download the zip file locally. Do not unzip this file.
-
Import the shapefile into ArcGIS.
-
Sign in to your ArcGIS portal.
-
In the top navigation bar, click Content.
-
Click New item. To upload the Santa Monica Parcels shapefile, you can either:
- Drag and drop the file.
- Or, click Your device and navigate to the file path.
-
Select Add Santa Monica Parcels.zip to publish the file as a hosted feature layer.
-
In Fields, leave all fields at their default settings and click Next.
-
In Location settings, leave the default settings and click Next.
-
Set the following information in the item details pane:
- Title:
Santa Monica Parcels - Tags:
Santa MonicaParcels. - Summary:
Parcels in the Santa Monica Mountains.
- Title:
-
Click Next to create the new feature layer and feature service.
-
In the feature service item page, make sure the Share setting is set to Owner.
-
Scroll down to the URL section and copy the URL into a safe location. You will use this in a later step. The URL will look something like:
Use dark colors for code blocks Copy https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Parcels/FeatureServer
-
Create a new app
-
Open a terminal and create a new folder for your project.
Use dark colors for code blocks Copy mkdir get-layer-metadata cd get-layer-metadata -
Initialize a new Node.js project. This creates a
package.jsonfile.Use dark colors for code blocks Copy npm init -
Install the required packages.
Use dark colors for code blocks Copy npm install @esri/arcgis-rest-request @esri/arcgis-rest-feature-service --save -
Create a new JavaScript file named
index.js.Use dark colors for code blocks Copy touch index.js
Get an access token
Create a new API key credential with the correct privileges to get an access token.
- Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Item access
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials access to the layer item. Learn more in Item access privileges.
- Item access
- Copy the API key access token to your clipboard when prompted.
Make a request
-
Open your
index.jsfile and import the library.index.jsUse dark colors for code blocks import { ApiKeyManager } from "@esri/arcgis-rest-request"; import { getService } from "@esri/arcgis-rest-feature-service"; -
Paste in your access token.
index.jsUse dark colors for code blocks import { ApiKeyManager } from "@esri/arcgis-rest-request"; import { getService } from "@esri/arcgis-rest-feature-service"; const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(accessToken); -
Make a request to your feature layer and print the results.
index.jsUse dark colors for code blocks import { ApiKeyManager } from "@esri/arcgis-rest-request"; import { getService } from "@esri/arcgis-rest-feature-service"; const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(accessToken); getService("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0", { authentication }).then((response) => { console.log(JSON.stringify(response, null, 2)); }); -
Save the file, then run it from the terminal.
Use dark colors for code blocks Copy node index.js
You should now see the results printed in your console.
{
"currentVersion": 11,
"id": 0,
"name": "Trailheads_Styled",
"type": "Feature Layer",
"serviceItemId": "2e4b3df6ba4b44969a3bc9827de746b3",
"cacheMaxAge": 30,
"displayField": "",
"description": "",
"copyrightText": "",
"defaultVisibility": true,
"editingInfo": {
"lastEditDate": 1556814539714,
"schemaLastEditDate": 1556814539714,
"dataLastEditDate": 1556814539714
What's next?
Learn how to use additional location services in these tutorials: