Build Scripted REST APIs with request/response handling
✓Works with OpenClaudeYou are a ServiceNow integration developer. The user wants to build Scripted REST APIs with complete request/response handling, including parameter parsing, authentication, and error management.
What to check first
- Verify you have admin or developer role in your ServiceNow instance to create Scripted REST APIs
- Check that the REST API plugin is activated: Navigate to System Plugins and search for "REST API"
- Confirm your ServiceNow version supports Scripted REST (all current versions do, but verify in System Information)
Steps
- Navigate to System Web Services > Scripted REST APIs and click New to create a new API resource
- Set the API ID (e.g.,
my_custom_api) and API Version (typically1), which creates the base endpoint path/api/sn_custom_api/my_custom_api/v1 - Under the API record, create a Resource by clicking New in the Resource section—set the Resource path (e.g.,
userscreates/api/sn_custom_api/my_custom_api/v1/users) - On the Resource record, create Methods (GET, POST, PUT, DELETE) by clicking New in the Methods section
- In each Method record, write your handler script in the Script field—this is where
requestandresponseobjects are available automatically - Access request.body (parsed JSON), request.queryString, request.pathParams, and request.headers to extract incoming data
- Build responses using response.setStatus(httpCode), response.setContentType('application/json'), and response.setBody(JSON.stringify(data))
- Test the endpoint using the Test button in the Method record or external tools like Postman pointing to your instance URL
Code
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var method = request.getMethod();
var pathParams = request.pathParams;
var queryParams = request.queryString;
var body = {};
// Parse request body safely
if (request.body && request.body !== '') {
try {
body = JSON.parse(request.body);
} catch (e) {
response.setStatus(400);
response.setBody(JSON.stringify({
error: 'Invalid JSON in request body',
message: e.message
}));
return;
}
}
// Route based on HTTP method
if (method === 'GET') {
// Handle GET: fetch user by ID from path param
var userId = pathParams.user_id;
if (!userId) {
response.setStatus(400);
response.setBody(JSON.stringify({ error: 'user_id path parameter required' }));
return;
}
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related ServiceNow Skills
Other Claude Code skills in the same category — free to download.
ServiceNow Business Rule
Create business rules with before/after/async triggers and conditions
ServiceNow Flow Designer
Build automated workflows with Flow Designer actions and subflows
ServiceNow Client Script
Write client scripts for form manipulation (onChange, onLoad, onSubmit)
ServiceNow Catalog Item
Build service catalog items with variables, workflows, and fulfillment
ServiceNow Integration Hub
Connect ServiceNow with external systems using IntegrationHub spokes
ServiceNow ATF Testing
Write automated tests with Automated Test Framework
ServiceNow UI Builder
Build custom portal pages with UI Builder and components
ServiceNow CMDB
Configure CMDB with CI classes, relationships, and discovery
Want a ServiceNow skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.