Learn how to access and query a hosted feature layer with SQL queries.
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 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 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 { 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 where the land use type is
Residentialand 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); queryFeatures({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0", where: "UseType = 'Residential'", resultRecordCount: 1, 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.
[
{
"attributes": {
"OBJECTID": 52,
"AIN": "2004001003",
"APN": "2004-001-003",
"SitusHouseNo": "8321",
"SitusFraction": " ",
"SitusDirection": " ",
"SitusUnit": " ",
"SitusStreet": "FAUST AVE",
"SitusAddress": "8321 FAUST AVE",
"SitusCity": "LOS ANGELES CA",
"SitusZIP": "91304-3327",
"SitusFullAddress": "8321 FAUST AVE LOS ANGELES CA 91304",
What's next?
Learn how to use additional location services in these tutorials: