I am trying to create an empty Hosted Feature service from my app using the ArcGIS REST API. I want that service to have multiple layers, and the fields would be dynamically created at creation type.
One would think that the createService API call (https://developers.arcgis.com/rest/users-groups-and-items/create-service/) would be the right thing to look at, right? Unfortunately, it looks like ESRI wants only the admins to do this. The documentation says: "This endpoint is only accessible to the content owner and members who are assigned the default administrator role. All other members will receive a permissions error when attempting to access this endpoint."
So then I looked into how ArcGIS Online does it and looks like they use a combination of /addItem (https://developers.arcgis.com/rest/users-groups-and-items/add-item/) and /publish (https://developers.arcgis.com/rest/users-groups-and-items/publish-item/) to achieve this.
To achieve what I need, I would have to first create a file GDB with my required layers, upload that, and then publish it. And this would mean that an uploaded file geodatabase is left as a file item, which just feels icky.
Is there a better way to do what I need to do?
2 Answers 2
I agree the API reference isn't great at explaining the whole workflow. Let me try to help.
This endpoint is only accessible to the content owner and members who are assigned the default administrator role
To use that endpoint, you need to use a token created with permissions to create hosted content (like an editor user type in ArcGIS Online).
I have done that by creating the service schema manually (sending a JSON schema definition).
So then I looked into how ArcGIS Online does it and looks like they use a combination of /addItem (https://developers.arcgis.com/rest/users-groups-and-items/add-item/) and /publish (https://developers.arcgis.com/rest/users-groups-and-items/publish-item/) to achieve this.
Some time ago, I created this Postman collection to demo the whole workflow. I would be something like:
- Check that the service doesn't already exist (REST call)
- Create the item
targetType=featureService
(e.g., REST call) - Add the schema definition to the service (e.g., REST call creating a point layer)
You can also check the Define a new feature layer tutorial included in the Portal and data services guide.
Update: I just a blog article that explains the different approaches to create hosted feature services among other things.
I hope this helps!
-
I had figured out that we had to do something like this. But turns out that you can do this only with an Oauth Token, and not an API key . I had posted this on the ESRI forums: community.esri.com/t5/arcgis-rest-apis-and-services-questions/… But haven't got a resolution there.Devdatta Tengshe– Devdatta Tengshe2025年09月25日 03:51:06 +00:00Commented Sep 25 at 3:51
You don't need to upload an fGDB first. Even if you do, you can delete the fGDB from AGOL in code.
Here's how I publish (overwrite) without uploading an fGDB:
arcpy.mp.CreateWebLayerSDDraft(mp, sddraft_file, map_name, overwrite_existing_service=True)
print(4*" " + f"Staging local Service Definition: {sd_file}")
arcpy.StageService_server(sddraft_file, sd_file)
print(4*" " + f"Overwriting online service definition: {sd_item.title}")
sd_item.update(data=sd_file)
print(4*" " + f"Publishing feature service")
pub_params = {"editorTrackingInfo": {"enableEditorTracking": 'true', "preserveEditUsersAndTimestamps": 'true'}}
fs = sd_item.publish(publish_parameters=pub_params, overwrite=True)
You should be able to tweak this slightly to publish a new service, rather than overwrite an existing one.
Note that instead of leaving a fGDB sitting in AGOL, this will leave a service definition sitting in AGOL. Again, you can delete this (in code) if you wish. However, that would mean you could no longer overwrite the feature service in-place.
createService
and then carry on withaddToDefinition
,update
andupdateItems
as needed. (Open browser dev tools and follow the flow while logged in as a publisher using Content > New Item > Feature Layer)