Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@pagerduty/pdjs
Advanced tools
@pagerduty/pdjs is an npm package that provides a JavaScript client for interacting with the PagerDuty REST API. It allows developers to integrate PagerDuty's incident management and on-call scheduling functionalities into their applications.
Create an Incident
This feature allows you to create a new incident in PagerDuty. The code sample demonstrates how to use the @pagerduty/pdjs package to create an incident with a specified title, service, and urgency.
const { pd } = require('@pagerduty/pdjs');
const pdClient = pd({ token: 'YOUR_API_TOKEN' });
pdClient.post('/incidents', {
data: {
incident: {
type: 'incident',
title: 'Example Incident',
service: {
id: 'SERVICE_ID',
type: 'service_reference'
},
urgency: 'high'
}
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
List Incidents
This feature allows you to list all incidents in PagerDuty. The code sample demonstrates how to use the @pagerduty/pdjs package to retrieve and log a list of incidents.
const { pd } = require('@pagerduty/pdjs');
const pdClient = pd({ token: 'YOUR_API_TOKEN' });
pdClient.get('/incidents').then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
Acknowledge an Incident
This feature allows you to acknowledge an existing incident in PagerDuty. The code sample demonstrates how to use the @pagerduty/pdjs package to change the status of an incident to 'acknowledged'.
const { pd } = require('@pagerduty/pdjs');
const pdClient = pd({ token: 'YOUR_API_TOKEN' });
pdClient.put('/incidents/INCIDENT_ID', {
data: {
incident: {
type: 'incident_reference',
status: 'acknowledged'
}
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
Resolve an Incident
This feature allows you to resolve an existing incident in PagerDuty. The code sample demonstrates how to use the @pagerduty/pdjs package to change the status of an incident to 'resolved'.
const { pd } = require('@pagerduty/pdjs');
const pdClient = pd({ token: 'YOUR_API_TOKEN' });
pdClient.put('/incidents/INCIDENT_ID', {
data: {
incident: {
type: 'incident_reference',
status: 'resolved'
}
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
node-pagerduty is another npm package for interacting with the PagerDuty API. It provides similar functionalities to @pagerduty/pdjs, such as creating, listing, acknowledging, and resolving incidents. However, @pagerduty/pdjs is the official PagerDuty client library, which may offer more up-to-date and comprehensive support for the latest API features.
A simple JavaScript wrapper for the PagerDuty APIs.
For full API Reference see this page.
npm install --save @pagerduty/pdjs
REST API calls can be done using the convenience methods or by passing in a url
or endpoint
.
There are some simple convience methods. get()
, post()
, put()
, and delete()
.
import {api} from '@pagerduty/pdjs';
const pd = api({token: 'someToken1234567890'});
pd.get('/incidents')
.then({data, resource, next} => console.log(data, resource, next))
.catch(console.error);
// Similarly, for `post`, `put`, `patch` and `delete`.
pd.post('/incidents', { data: { ... } }).then(...)
tokenType
Allows you to set either token
or bearer
tokens. Defaults to token
but provides ability to use bearer
as well.
api
you'll pass int the tokenType
parameter, like so:const pd = api({token: 'someBearerToken1234567890', tokenType: 'bearer'});
url
or endpoint
// Calling the returned function with a `endpoint` or `url` will also send it.
pd({
method: 'post',
endpoint: '/incidents',
data: {
...
}
}).then(...)
The PD object always returns an APIResponse object which contains some raw data as well as a convenient shortcut to directly access the returns Resource.
pd.get('/incidents')
.then({resource, data, response, next} => {
console.log(resource); // Contains the data of resource request. In this example the 'incidents' data.
console.log(data); // The raw data returned from the API, also contains pagination information.
console.log(response); // The response object returned from the cross-fetch
console.log(next); // A convenience function to help with pagination
})
.catch(console.error);
There's an convience method all
that attempts to fetch all pages for a given endpoint and set of parameters. For convenience this function supports both offset and cursor based pagination.
Note that the PagerDuty API has a limit for most endpoints and recommends using parameters to refine searches where more results are necessary. More information can be found in the Developer Documentation.
The response object of an API call also contains a nextFunc
which can be used to define your own Pagination function if you feel included.
import {api} from '@pagerduty/pdjs';
const pd = api({token: 'someToken1234567890'});
pd.all('/incidents')
.then({data, resource} => console.log(data, resource))
.catch(console.error);
The API interface allows for some extra parameters to be included.
server
To use this library with a different service region use this parameter to change the root url of requests. Default: api.pagerduty.com
.
pd({
method: 'post',
endpoint: '/incidents',
server: 'api.eu.pagerduty.com',
data: {
...
}
}).then(...)
headers
Some endpoints require the setting of extra headers
such as a From
header.
pd({
method: 'post',
endpoint: '/incidents',
headers: {
'From': "me@example.com"
},
data: {
...
}
}).then(...)
There is some very simple retry logic baked into each request in the case that the PagerDuty API rate limits your requests (only when it responds HTTP Code 429). Requests will retry 3 times waiting 20 seconds between each request. If the request is still being rate limited after 3 attempts the client will simply return the 429 response.
Events V2 is supported along with Change Events.
import {event} from '@pagerduty/pdjs';
event({
data: {
routing_key: 'YOUR_ROUTING_KEY',
event_action: 'trigger',
dedup_key: 'test_incident_2_88f520',
payload: {
summary: 'Test Event V2',
source: 'test-source',
severity: 'error',
},
},
})
.then(console.log)
.catch(console.error);
import {change, trigger, acknowledge, resolve} from '@pagerduty/pdjs';
change({
"routing_key": "YOUR_ROUTING_KEY",
"payload": {
"summary": "Build Success:!",
"timestamp": "2015-07-17T08:42:58.315+0000",
"source": "prod-build-agent",
"custom_details": {
"build_state": "passed",
"build_number": "220",
"run_time": "1337s"
}
},
"links": [{
"href": "https://buildpipeline.com",
"text": "View in Build Pipeline"
}]
})
.then(console.log)
.catch(console.error);
trigger({...})
.then(console.log)
.catch(console.error);
acknowledge({...})
.then(console.log)
.catch(console.error);
resolve({...})
.then(console.log)
.catch(console.error);
Two browser-ready scripts are provided:
fetch
.fetch
polyfill -- mostly IE 11.Either of these files can be used by copying them into your project and including them directly, with all functions namespaced PagerDuty
:
<script src="https://raw.githubusercontent.com/PagerDuty/pdjs/main/dist/pdjs.js"></script>
<script>
PagerDuty.api({token: 'someToken1234567890', endpoint: '/incidents'})
.then(response => console.log(response.data))
.catch(console.error);
</script>
FAQs
A new simple JavaScript wrapper for the PagerDuty API
We found that @pagerduty/pdjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.