Bootstrap FreeKB - ServiceNow - Create alerts and events using REST API (curl)
ServiceNow - Create alerts and events using REST API (curl)

Updated:   |  ServiceNow articles

This assumes you have generated an OAuth Bearer Token using the ServiceNow REST API.

An alert may contain one or more events.

 

If there is already an alert in ServiceNow that is an exact match of the "type" and "resource" and the state of the alert is "Open", this command will add an event to the alert. On the other hand, this command will create a new alert with a single event.

curl
--insecure
--request POST
--header "Accept: application/json"
--header "Content-Type: application/json"
--header "Authorization: Bearer <your OAuth Bearer token goes here>"
--url https://example.service-now.com/api/global/em/inbound_event?source=genericJson 
--data '{ 
  "type": "Type field in ServiceNow",
  "source_instance": "something unique",
  "resource": "something unique",
  "node": "must be a valid ServiceNow Hardware Configuration Item (CI)",
  "description": "Example Message"
}'

 

You should be able to see the alert in ServiceNow.

 

Or like this, where "source" is set as a header instead of being passed in with the URL.

curl
--insecure
--request POST
--header "Accept: application/json"
--header "Content-Type: application/json"
--header "Authorization: Bearer <your OAuth Bearer token goes here>"
--header "Source: inbound_event v1"
--url https://example.service-now.com/api/global/em/inbound_event 
--data '{ 
  "type": "Type field in ServiceNow",
  "source_instance": "something unique",
  "resource": "something unique",
  "node": "must be a valid ServiceNow Hardware Configuration Item (CI)",
  "description": "Example Message" 
}'

 

Or the data could be stored in a JSON file, such as data.json.

{ 
  "type": "Type field in ServiceNow",
  "source_instance": "something unique",
  "resource": "something unique",
  "node": "must be a valid ServiceNow Hardware Configuration Item (CI)",
  "description": "Example Message"
}

 

And then the following command could be used.

curl
--insecure
--request POST
--header "Accept: application/json"
--header "Content-Type: application/json"
--header "Authorization: Bearer <your OAuth Bearer token goes here>"
--header "Source: inbound_event v1"
--url https://example.service-now.com/api/global/em/inbound_event 
--data @data.json

 


Resolution State

Optionally, "resolution_state" can be included to set the state of the event to New or Closing. If "resoultion_state" is not included, this will default to New. This has no impact on the state of the alert.

{ 
  "type": "Type field in ServiceNow",
  "source_instance": "something unique",
  "resource": "something unique",
  "node": "must be a valid ServiceNow Hardware Configuration Item (CI)",
  "description": "Example Message",
  "resolution_state": "New"
}

 


Incident

If you want to also create an incident ticket, include the incident block.

{ 
  "type": "Type field in ServiceNow",
  "source_instance": "something unique",
  "resource": "something unique",
  "node": "must be a valid ServiceNow Hardware Configuration Item (CI)",
  "description": "Example Message",
  "incident": { 
    "cmdb_ci": "Configuration Item in ServiceNow" 
  }
}

 


Notification

If you want to also send an email notification, include the notification block. The "recipient" must be a valid Assignement Group in ServiceNow.

{ 
  "type": "Type field in ServiceNow",
  "source_instance": "something unique",
  "resource": "something unique",
  "node": "must be a valid ServiceNow Hardware Configuration Item (CI)",
  "description": "Example Message",
  "notification": { 
    "recipient": "Assignment Group in ServiceNow",
    "subject": "Optional, email subject",
    "body": "Optional, email body",
    "response_required": true|false (defaults to false if not used),
    "email_only": true|false (defaults to false if not used)
  } 
}

 

If the request is successful, something like this should be returned.

{
  "result":{ "Transform Generic Events Instance": "success" }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 70956f in the box below so that we can be sure you are a human.