Create and configure a scripted REST API for your custom chat integration
- UpdatedAug 3, 2023
- 3 minutes to read
- Vancouver
- Virtual Agent
Create a scripted REST API, add a scripted REST resource, set security and content negotiation, and set REST API rate limits.
Before you begin
Map rich controls to the channel in your custom chat integration.
Role required: admin
Procedure
-
Create the REST API.
- Navigate to All > System web services > Scripted Web Services > Scripted REST APIs.
- Click New.
-
On the form, fill in the fields.
Field Description Name Name of your API. For example, ACME Mobile App Chat Adapter. API ID API identifier. For example, acme_chat. Protection policy The protection policy for the script. - Read-only: Script is viewable only.
- Protected: Users with password permissions can edit the script.
Application The application containing the script record. Global is selected by default. API namespace The namespace the API belongs to. The value depends on the current application scope. - Click Submit.
- (Optional)
Open the new record, navigate to Related Links
and click Enable Versioning, and then click
OK.
Click Update to save your changes.
-
Add a scripted REST resource to your new REST API.
The scripted REST resource defines the scripted REST API definition that you created.
- Open the REST API record you created, and then navigate to the Resources tab under Related Links.
-
Click New.
Retrieve the payload from the request, and then write it to a hybrid queue.
-
On the form, fill in the fields.
Field Description API Definition Name of the parent API. Application The application containing the script record. Name Name of your API REST Resource. For example, ACME chat. API version API version. For example, v1. This field displays only if you enabled versioning for the REST API.
Active Option to make the REST resource active. HTTP method HTTP method, such as POST, GET, and so forth. Relative path Relative path to the resource. Script Script for the REST resource. Protection policy The protection policy for the script. - Read-only: Script is viewable only.
- Protected: Users with password permissions can edit the script.
Example of a scripted REST resource:(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { var body = request.body; var queryParams = request.queryParams; // incoming content is application/x-www-form-urlencoded in this example // get the provider application sys id. this can be done via a glide query using incoming data such as where the original message is being sent to. or it can be hard-coded such as this example. var providerAppId = "a5f8b75b7377001042281188caf6a73a"; // the time of receipt is recorded for analytics purposes var d = new Date(); var logTime = d.getTime(); // add this message to the VA Server queue for processing var queued = sn_cs.VASystemObject.enqueueCustomAdapterMessage(providerAppId, JSON.stringify(queryParams), JSON.stringify(request.headers), logTime); if (queued == false) { response.setError(new sn_ws_err.BadRequestError('Failed to process the request.')); } })(request, response);
-
Set security and content negotiation for your scripted REST resource.
Choose to set authentication and request formats. If your custom integration does not rely on authentication, you may want to remove it as follows.
- In the new record for the Scripted REST Resource, navigate to the Security tab.
- Clear Requires authentication.
- Click the Content Negotiation tab, and then select Override supported request formats.
- Click Submit.
-
Set REST API rate limits to define the rate of incoming requests.
- Navigate to All > System web services > REST > Rate Limit Rules.
- Click New.
-
On the form, fill in the fields.
Field Description Name Unique name for the rate limit rule. REST API REST API that you created in an earlier step. Version Version of the REST API. Values listed depend on the selected REST API. Resource Resource for the specified Version. Values listed depend on the selected Version. Active Check box to indicate that the rate limit rule is active. Rate limit rules are activated by default as soon as you create them. You can deactivate rate limit rules to stop enforcing a rate limit or activate rate limit rules to resume enforcing a rate limit.
Request limit per hour Maximum number of requests allowed per hour. Note: Whenever you update the value of this field, the Now Platform resets the count of requests to 0 and deletes all violations for the current hour.Apply to Users restricted by this rule. Select All users. - Click Submit.
What to do next