Learn how to access and query a hosted feature layer
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 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
A hosted feature layer is a hosted layer (item) in a portal that is used to manage the properties and settings of a spatially-enabled feature layer in a feature service. . -
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
A feature layer (client-side) is a data layer that can access and display features from a feature service that has the same type of geometry and attribute fields. and feature serviceA feature service is a data service that provides access to spatial and non-spatial data in feature layers, feature layer views, and tables. . -
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 query-a-feature-layer cd query-a-feature-layer -
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
- Go to the Create an API key tutorial and create an API key with the following privilege(s)
Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. :
- Item access
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials
API key credentials are an item that contains the parameters used to create and manage long-lived access tokens for API key authentication. They are a type of developer credential. access to the layer item. Learn more in Item access privileges.
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials
- 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 { queryFeatures } 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 { queryFeatures } from "@esri/arcgis-rest-feature-service"; const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(accessToken); -
Query features that intersect a point and print the results to the console.
index.jsUse dark colors for code blocks import { ApiKeyManager } from "@esri/arcgis-rest-request"; import { queryFeatures } from "@esri/arcgis-rest-feature-service"; const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(accessToken); const queryGeometry = { x: -118.807, y: 34.002, spatialReference: { wkid: 4326 } }; queryFeatures({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0", geometry: queryGeometry, geometryType: "esriGeometryPoint", spatialRel: "esriSpatialRelIntersects", authentication }).then((response) => { console.log(response.features); }); -
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.
[
{
"attributes": {
"OBJECTID": 985600,
"AIN": "4468002902",
"APN": "4468-002-902",
"SitusHouseNo": "0",
"SitusFraction": " ",
"SitusDirection": " ",
"SitusUnit": " ",
"SitusStreet": " ",
"SitusAddress": "",
"SitusCity": " ",
"SitusZIP": " ",
"SitusFullAddress": "",
What's next?
Learn how to use additional location services in these tutorials: