Learn how to add, update, and delete data in 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 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 -
-
In the feature layer item page, go to Settings.
-
Under Feature layer (hosted), check the Enable editing box.
-
Save your changes.
Create a new app
-
Open a terminal and create a new folder for your project.
Use dark colors for code blocks Copy mkdir edit-feature-data cd edit-feature-data -
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 { addFeatures, updateFeatures, deleteFeatures } 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 { addFeatures, updateFeatures, deleteFeatures } from "@esri/arcgis-rest-feature-service"; const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = ApiKeyManager.fromKey(accessToken); -
Add a new feature to your layer and print the results to the console.
index.jsUse dark colors for code blocks const featureServiceLayerUrl = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/ArcGIS/rest/services/my_points/FeatureServer/0"; const featureToAdd = { attributes: { id: 101, name: "editing test", rating: "2" }, geometry: { x: -118.807, y: 34.002, spatialReference: { wkid: 4326 } } }; addFeatures({ url: featureServiceLayerUrl, features: [featureToAdd], authentication }).then(async (response) => { console.log(JSON.stringify(response, null, 2)); if (!response.addResults[0].success) { return; } }); -
Update the feature you just added and print the results to the console.
index.jsUse dark colors for code blocks const featureToUpdate = { attributes: { objectId: response.addResults[0].objectId, name: "new name", rating: "3" } }; const updateResponse = await updateFeatures({ url: featureServiceLayerUrl, features: [featureToUpdate], authentication }); console.log(updateResponse); if (!updateResponse.updateResults[0].success) { return; } -
Delete the updated feature and print the results to the console.
index.jsUse dark colors for code blocks const featureToDelete = [updateResponse.updateResults[0].objectId]; const deleteResponse = await deleteFeatures({ url: featureServiceLayerUrl, objectIds: [featureToDelete], authentication }); console.log(deleteResponse); -
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.
[
{
"addResults": [
{
"objectId": 5948,
"uniqueId": 5948,
"globalId": null,
"success": true
}
]
},
{
"updateResults": [
{
"objectId": 5948,
What's next?
Learn how to use additional location services in these tutorials: