Skip to content

Learn how to access and query a hosted feature layer with SQL 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
    
    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
    
    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 where the land use type is Residential 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
    
    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));
    });
  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
[
  {
    "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",
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.