Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

scripted rest api help

Prakash_S
Tera Contributor

I have to implement scripted rest api for bi-directional integration with third part tool. can someone guide me step by step to create it.

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

Follow below link, it has step by step guide to build scripted rest API.

https://servicenowwithrunjay.com/scripted-rest-api-servicenow/

 

You can also watch the live implementation of Scripted REST API with real time use case here: https://www.youtube.com/watch?v=Nm-niuGXaww&t=2083s&ab_channel=ServiceNowWithRunjay

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...
2 REPLIES 2

a5hubh
Tera Expert

Implementing a ServiceNow Scripted REST API for bi-directional integration with a third-party tool involves several steps. Here's a detailed guide to help you create it:

Step 1: Define the Requirements

  1. Identify the Data: Determine what data needs to be exchanged between ServiceNow and the third-party tool.
  2. Integration Points: Identify the integration points for both inbound (third-party to ServiceNow) and outbound (ServiceNow to third-party) communications.
  3. Security Requirements: Ensure you know the authentication and authorization mechanisms required for secure communication.

Step 2: Set Up ServiceNow Environment

  1. Access ServiceNow: Log in to your ServiceNow instance with administrative privileges.
  2. Navigate to Scripted REST APIs: Go to System Web Services > Scripted REST APIs.

Step 3: Create a New Scripted REST API

  1. Create API:

    • Click New to create a new API.
    • Fill in the fields:
      • Name: Name your API.
      • API ID: Unique identifier for your API.
      • API Version: Specify the version (e.g., v1).
  2. Save the API: Click Submit.

Step 4: Define Resources

  1. Add a Resource:

    • In the newly created API, scroll down to the Resources tab.
    • Click New to add a new resource.
    • Fill in the fields:
      • Name: Name your resource (e.g., GetIncident).
      • HTTP Method: Select the HTTP method (e.g., GET, POST).
  2. Script the Resource:

    • In the resource script field, write the script to handle the request.
    • Example for GET method to fetch an incident:

 

 

(function process(request, response) {
    var incidentId = request.queryParams.sys_id;
    var incidentGR = new GlideRecord('incident');
    if (incidentGR.get(incidentId)) {
        var responseBody = {
            sys_id: incidentGR.sys_id.toString(),
            number: incidentGR.number.toString(),
            short_description: incidentGR.short_description.toString()
        };
        response.setBody(responseBody);
    } else {
        response.setStatus(404);
        response.setError("Incident not found");
    }
})(request, response);

 

 

  • Save the Resource: Click Submit.

Step 5: Handle Authentication

  1. Enable Authentication:
    • Go to the Authentication tab in your API.
    • Choose the appropriate authentication method (e.g., Basic Auth, OAuth).

Step 6: Test the API

  1. Test Using REST API Explorer:
    • Go to System Web Services > REST API Explorer.
    • Select your API and the resource you created.
    • Test the API by making requests and verifying the responses.

Step 7: Implement Inbound Integration

  1. Set Up Endpoint in Third-Party Tool:

    • Configure the third-party tool to send data to the ServiceNow API endpoint.
  2. Receive and Process Data in ServiceNow:

    • Write scripts in your ServiceNow resource to handle the incoming data and perform necessary actions.

Step 8: Implement Outbound Integration

  1. Create Business Rules or Scheduled Jobs:
    • Go to System Definition > Business Rules to create a new business rule.
    • Write scripts to send data from ServiceNow to the third-party tool using outbound REST messages.
    • Example:
       

 

 

var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://thirdparty.example.com/api/endpoint');
r.setHttpMethod('POST');
r.setRequestHeader('Content-Type', 'application/json');
var requestBody = {
    incident_id: current.sys_id.toString(),
    short_description: current.short_description.toString()
};
r.setRequestBody(JSON.stringify(requestBody));
var response = r.execute();

 

 

Step 9: Monitor and Troubleshoot

  1. Monitor Logs:

    • Use System Logs > System Log > All to monitor logs for any errors.
    • Use System Logs > Outbound HTTP Requests to check the status of outbound REST messages.
  2. Handle Errors:

    • Implement error handling in your scripts to manage and log errors.

Step 10: Documentation and Maintenance

  1. Document the API:

    • Ensure you document the API endpoints, request/response formats, and any required parameters.
  2. Regular Maintenance:

    • Regularly review and update the integration scripts as necessary.
    • Monitor the integration to ensure it continues to function correctly.

By following these steps, you can successfully create a ServiceNow Scripted REST API for bi-directional integration with a third-party tool.

 

If the above information helps you, kindly mark it as helpful and accept the solution.

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

Follow below link, it has step by step guide to build scripted rest API.

https://servicenowwithrunjay.com/scripted-rest-api-servicenow/

 

You can also watch the live implementation of Scripted REST API with real time use case here: https://www.youtube.com/watch?v=Nm-niuGXaww&t=2083s&ab_channel=ServiceNowWithRunjay

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...