- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2024 12:14 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2024 12:22 AM
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
-------------------------------------------------------------------------

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2024 12:19 AM - edited ‎11-29-2024 05:43 AM
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
- Identify the Data: Determine what data needs to be exchanged between ServiceNow and the third-party tool.
- Integration Points: Identify the integration points for both inbound (third-party to ServiceNow) and outbound (ServiceNow to third-party) communications.
- Security Requirements: Ensure you know the authentication and authorization mechanisms required for secure communication.
Step 2: Set Up ServiceNow Environment
- Access ServiceNow: Log in to your ServiceNow instance with administrative privileges.
- Navigate to Scripted REST APIs: Go to System Web Services > Scripted REST APIs.
Step 3: Create a New Scripted REST API
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).
Save the API: Click Submit.
Step 4: Define Resources
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).
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
- 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
- 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
Set Up Endpoint in Third-Party Tool:
- Configure the third-party tool to send data to the ServiceNow API endpoint.
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
- 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
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.
Handle Errors:
- Implement error handling in your scripts to manage and log errors.
Step 10: Documentation and Maintenance
Document the API:
- Ensure you document the API endpoints, request/response formats, and any required parameters.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2024 12:22 AM
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
-------------------------------------------------------------------------