Manage KV store collections and data with the Splunk REST API
You can use the Splunk REST API to programmatically manage KV store collections and data for Splunk Cloud Platform and Splunk Enterprise apps. By using the REST API, you can decrease the need for manual configuration through the web interface and support automation.
When using the Splunk REST API, use the splunkd management port (8089) and the secure HTTPS protocol.
Splunk REST API endpoints for KV store
You can use the following REST API endpoints to help you work with KV store collections and data:
Endpoint | Method | Description |
|---|---|---|
storage/collections/config | GET POST | Returns a list of collections in a specific app. Creates a new collection in a specific app. |
storage/collections/config/<collection> | GET POST DELETE | Returns information about a specific collection. Updates a collection. Removes a collection. |
storage/collections/data/<collection>/ | GET POST DELETE | Returns records from a specific collection. Creates a new record in a specific collection. Removes all records from a specific collection. |
storage/collections/data/<collection>/<id> | GET POST DELETE | Returns records in a collection by key ID. Updates a record in a collection by key ID. Removes a record in a collection by key ID. |
storage/collections/data/<collection>/batch_save | POST | Performs multiple save operations on records in a specific collection. |
kvstore/backup/create | POST | Creates a KV store backup archive file. This endpoint is available only for Splunk Enterprise. |
kvstore/backup/restore | POST | Activates the extraction of the KV store backup archive file and restores the KV store. This endpoint is available only for Splunk Enterprise. |
kvstore/status | GET | Returns the KV store status information for standalone or search head clustering (SHC) deployments. |
For detailed information about the KV store API, see the KV store endpoint descriptions(Opens new window).
Examples
The following curl examples guide you through a series of common KV store management tasks, from creating a collection to running bulk operations. These examples show how to create and manage a sample kvstorecoll collection in the existing kvstoretest app, with a basic <username>:<password> authentication of admin:yourpassword, and the <user>/<app> context of nobody/kvstoretest.
By default, the Splunk REST API returns responses in XML format. The sample responses in this document use JSON format for readability. To receive JSON responses, include the -d 'output_mode=json' parameter in your requests.
Check existing collections
Check what collections already exist in the kvstoretest app:
curlcurl -k -u admin:yourpassword --get \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/config
Sample response:
json{"entry": [{"name": "kvstorebuttercup","id": "https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/config/kvstorebuttercup","content": {"eai:appName": "kvstoretest","eai:userName": "nobody",
Create a new collection
Create a new kvstorecoll collection in the kvstoretest app:
curlcurl -k -u admin:yourpassword \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/config \-d 'name=kvstorecoll'
Sample response:
json{"entry": [{"name": "kvstorecoll","id": "https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/config/kvstorecoll","content": {"eai:appName": "kvstoretest","eai:userName": "nobody"
Define the collection schema
Define the field types and any accelerated fields for better query performance:
curlcurl -k -u admin:yourpassword \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/config/kvstorecoll \-d 'field.id=number' \-d 'field.name=string' \-d 'field.address=string' \-d 'accelerated_fields.my_accel={"id": 1}'
Sample response:
json{"entry": [{"name": "kvstorecoll","id": "https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/config/kvstorecoll","content": {"eai:appName": "kvstoretest","eai:userName": "nobody",
Add data to your collection
Add records to your collection:
curlcurl -k -u admin:yourpassword \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll \-H 'Content-Type: application/json' \-d '{"name": "Splunk HQ", "id": 123, "address": { "street": "250 Brannan Street", "city": "San Francisco", "state": "CA", "zip": "94107"}}'
Sample response:
json{"_key": "5410be5441ba15298e4624d1"}
Return all data from your collection
Return all collection records to validate the added data:
curlcurl -k -u admin:yourpassword --get \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll
Sample response:
json[{"_key": "5410be5441ba15298e4624d1","_user": "nobody","name": "Splunk HQ","id": 123,"address": {"street": "250 Brannan Street",
Query data with pagination and sorting
The following pagination and sorting example assumes you added more records to the kvstorecoll collection.
For larger collections, return specific ranges of records with sorting. This example skips the first 10 records and returns the next 3 records at positions 11 to 13, sorted alphabetically by name:
curlcurl -k -u admin:yourpassword --get \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll \-d 'sort=name' \-d 'skip=10' \-d 'limit=3'
Sample response showing records at positions 11 to 13 in the sorted collection:
json[{"_key": "5410be5441ba15298e4624d2","_user": "nobody","name": "Buttercup Games A","id": 11},{
Filter data with queries
Return filtered records by using MongoDB-style query syntax. This example returns records where values in the id field are greater than 24, using the MongoDB query syntax of {"id": {"$gt": 24}}:
curlcurl -k -u admin:yourpassword --get \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll \-d 'query={"id": {"$gt": 24}}'
Sample response:
json[{"_key": "5410be5441ba15298e4624d1","_user": "nobody","name": "Splunk HQ","id": 123,"address": {"street": "250 Brannan Street",
To learn how to use MongoDB queries with the Splunk REST API, see KV store endpoint descriptions(Opens new window).
Return a specific record
Return individual records by using their unique key ID:
curlcurl -k -u admin:yourpassword --get \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll/5410be5441ba15298e4624d1
Sample response:
json{"_key": "5410be5441ba15298e4624d1","_user": "nobody","name": "Splunk HQ","id": 123,"address": {"street": "250 Brannan Street","city": "San Francisco",
Update an existing record
Modify an existing record with its key ID and specify which fields to update:
curlcurl -k -u admin:yourpassword \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll/5410be5441ba15298e4624d1 \-H 'Content-Type: application/json' \-d '{"name": "new_name", "id": 123, "address": { "street": "250 Brannan Street", "city": "San Francisco", "state": "CA", "zip": "94107"}}'
Sample response:
json{"_key": "5410be5441ba15298e4624d1","_user": "nobody","name": "new_name","id": 123,"address": {"street": "250 Brannan Street","city": "San Francisco",
This command replaces all previous values in the specified record. If you don’t include values for fields with existing values, the command replaces such field values with an empty string.
To update only the name field and remove other existing fields from the record, send a request that contains only the field that you want to update and keep:
curlcurl -k -u admin:yourpassword \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll/5410be5441ba15298e4624d1 \-H 'Content-Type: application/json' \-d '{"name": "new_name_only"}'
Sample response:
json{"_key": "5410be5441ba15298e4624d1","_user": "nobody","name": "new_name_only"}
Remove a specific record
Remove individual records by using their unique key ID:
curlcurl -k -u admin:yourpassword -X DELETE \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll/5410be5441ba15298e4624d1
Remove all records from a collection
Keep the collection structure while removing all existing records:
curlcurl -k -u admin:yourpassword -X DELETE \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll
Perform bulk save operations
Run multiple save operations in a batch to manage multiple records efficiently:
curlcurl -k -u admin:yourpassword \https://localhost:8089/servicesNS/nobody/kvstoretest/storage/collections/data/kvstorecoll/batch_save \-H 'Content-Type: application/json' \-d '[{"name": "Splunk Sweden"}, {"name": "Splunk Singapore"}]'
Sample response:
json["5f2a7b8c1234567890abcdef", "5f2a7b8c1234567890abcdeg"]