Skip to content

Learn how to access and query a hosted feature layer with spatial queries.

Prerequisites

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.

  1. In your web browser, go to the Santa Monica Parcels item.

  2. Click the Download button to download the zip file locally. Do not unzip this file.

  3. Import the shapefile into ArcGIS.

    1. Sign in to your ArcGIS portal.

    2. In the top navigation bar, click Content.

    3. 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.
    4. Select Add Santa Monica Parcels.zip to publish the file as a hosted feature layer.

    5. In Fields, leave all fields at their default settings and click Next.

    6. In Location settings, leave the default settings and click Next.

    7. Set the following information in the item details pane:

      • Title: Santa Monica Parcels
      • Tags: Santa Monica Parcels.
      • Summary: Parcels in the Santa Monica Mountains.
    8. Click Next to create the new feature layer and feature service.

    9. In the feature service item page, make sure the Share setting is set to Owner.

    10. 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 blocksCopy
      1
      https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Parcels/FeatureServer

Create a new app

  1. Open a terminal and create a new folder for your project.

    Use dark colors for code blocksCopy
    1
    2
    mkdir query-a-feature-layer
    cd query-a-feature-layer

  2. Initialize a new Node.js project. This creates a package.json file.

    Use dark colors for code blocksCopy
    1
    npm init
  3. Install the required packages.

    Use dark colors for code blocksCopy
    1
    npm install @esri/arcgis-rest-request @esri/arcgis-rest-feature-service --save

  4. Create a new JavaScript file named index.js.

    Use dark colors for code blocksCopy
    1
    touch index.js

Get an access token

Create a new API key credential with the correct privileges to get an access token.

  1. 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.
  2. Copy the API key access token to your clipboard when prompted.

Make a request

  1. Open your index.js file and import the library.

    index.js
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    import { ApiKeyManager } from "@esri/arcgis-rest-request";
    import { queryFeatures } from "@esri/arcgis-rest-feature-service";
    
    
  2. Paste in your access token.

    index.js
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    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);
    
    
  3. Query features that intersect a point and print the results to the console.

    index.js
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    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);
    });
  4. Save the file, then run it from the terminal.

    Use dark colors for code blocksCopy
    1
    node index.js

You should now see the results printed in your console.

Use dark colors for code blocksCopy
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
[
  {
    "attributes": {
      "OBJECTID": 985600,
      "AIN": "4468002902",
      "APN": "4468-002-902",
      "SitusHouseNo": "0",
      "SitusFraction": " ",
      "SitusDirection": " ",
      "SitusUnit": " ",
      "SitusStreet": " ",
      "SitusAddress": "",
      "SitusCity": " ",
      "SitusZIP": " ",
      "SitusFullAddress": "",
Expand

What's next?

Learn how to use additional location services in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.