As you build more complicated Splunk apps, you might wonder, “What is the best way to make the features in my app more usable?”
If you’re adding new SPL commands or creating ways to input new data sources, the answer is straightforward. But imagine you’re trying to address one of the following scenarios:
For cases like these, consider extending the Splunk REST API with custom endpoints. A custom REST endpoint is a developer-defined endpoint that lets you build out the Splunk REST API to meet your specific needs.
Note: You may have seen our new documentation about custom REST endpoints. Check out Extend the Splunk platform REST API with custom endpoints on the Splunk Developer Portal.
So, what can you do with custom REST endpoints? Here are two great uses:
When you make a request to an endpoint in the Splunk REST API, Splunk software routes your request to the REST handler defined in the restmap.conf file. To add a custom REST endpoint to this definition, create a restmap.conf file in your app that specifies the name and location of your endpoint. If you also want to make your endpoint accessible through the front end, create a web.conf file in your app to specify how to expose the endpoint in Splunk Web.
Note that for Splunk Cloud Platform customers to access REST API endpoints, they need to submit a Support request. See Accessing the Splunk Cloud REST API in the Splunk Enterprise REST API Tutorials.
Now, let’s walk through how to create a custom REST endpoint. In this example, we’ll create a custom REST handler that returns the payload {"text":"Hello world!"}.
First, we write the Python script for our custom REST handler, shown in the following image. We’ll save the script in the /bin directory of our app.
hello_world.py custom REST handler Python script
In this example, we import the PersistentServerConnectionApplication base Python class, which is provided in Splunk Enterprise.
Then, we create the HelloWorld child class. This class contains the __init__ method to accept the self, _command_line, and _command_arg variables.
The HelloWorld class also contains the handle method, which takes in incoming request arguments through the in_string parameter and returns the {"text":"Hello world!"} payload when the endpoint is called.
After we write the Python script, we need to map the script to an endpoint. We can map our REST handler to an endpoint in our app’s restmap.conf file, as shown in the following image.
Script stanza in restmap.conf
In this example, we create a script stanza and map this stanza to hello-world, the unique name associated with our custom REST handler.
Next, we specify other attributes for the REST handler. These attributes include the endpoint URI that the handler matches to, the script name, the script type, and the Python class that serves the API requests.
Now, we are ready to expose the endpoint on the Splunk Web server port. This step enables our users to call the endpoint in Splunk Web through an SPL search using the rest command. We can expose the endpoint in Splunk Web in our app’s web.conf file, depicted in the next image.
Expose stanza in web.conf
In this example, we create an expose stanza and map this stanza to hello-world, the name associated with our endpoint. Note that the endpoint name is unique across all apps in your Splunk Enterprise deployment.
Then, we specify the URI where we mounted the endpoint in the restmap.conf file and the supported HTTP methods for our handler.
Now, our app’s end users can call this endpoint in the following ways:
Now, you can try creating a custom REST endpoint for your own Splunk app!
See Use the script interface for custom REST endpoints on the Splunk Developer Portal for comprehensive documentation.
As always, if you have any questions or feedback, reach out at devfeedback@splunk.com.
Thank you to Amy Sutedja, Principal Software Engineer, for providing code samples.
The Splunk platform removes the barriers between data and action, empowering observability, IT and security teams to ensure their organizations are secure, resilient and innovative.
Founded in 2003, Splunk is a global company — with over 7,500 employees, Splunkers have received over 1,020 patents to date and availability in 21 regions around the world — and offers an open, extensible data platform that supports shared data across any environment so that all teams in an organization can get end-to-end visibility, with context, for every interaction and business process. Build a strong data foundation with Splunk.