0

Add Webhook

by
Published Oct 17, 2025

Add a webhook for the calling application.When registering a webhook, you will need to provide your own URI in the request body (see our full webhook documention here on how to configure your URI). The newly registered webhook will send notifications to this endpoint as JSON payloads which vary by domain. Please configure your endpoint to accept the relevant payloads. To see an example payload for each domain, please refer to the "Callbacks" tab.

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Add Webhook
4
 * Add a webhook for the calling application.When registering a webhook, you will need to provide your own URI in the request body (see our full webhook documention here on how to configure your URI). The newly registered webhook will send notifications to this endpoint as JSON payloads which vary by domain. Please configure your endpoint to accept the relevant payloads. To see an example payload for each domain, please refer to the "Callbacks" tab.
5
 */
6
export async function main(auth: RT.Paychex, body: Body) {
7
	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
8
	const url = new URL(`https://api.paychex.com/management/hooks`)
9

10
	const response = await fetch(url, {
11
		method: 'POST',
12
		headers: {
13
			'Content-Type': 'application/json',
14
			Authorization: 'Bearer ' + accessToken
15
		},
16
		body: JSON.stringify(body)
17
	})
18
	if (!response.ok) {
19
		const text = await response.text()
20
		throw new Error(`${response.status} ${text}`)
21
	}
22
	return await response.json()
23
}
24

25
async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
26
	const params = new URLSearchParams({
27
		grant_type: 'client_credentials'
28
	})
29

30
	const response = await fetch(tokenUrl, {
31
		method: 'POST',
32
		headers: {
33
			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
34
			'Content-Type': 'application/x-www-form-urlencoded'
35
		},
36
		body: params.toString()
37
	})
38

39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
42
	}
43

44
	const data = await response.json()
45
	return data.access_token
46
}
47

48
/* eslint-disable */
49
/**
50
 * This file was automatically generated by json-schema-to-typescript.
51
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
52
 * and run json-schema-to-typescript to regenerate this file.
53
 */
54

55
/**
56
 * This resource is for registering/unregistering webhooks.
57
 */
58
export interface Body {
59
	/**
60
	 * The unique identifier associated with this webhook. Not required for Posting.
61
	 */
62
	hookId?: string
63
	/**
64
	 * Enter a valid URL for the webhook to receive events.
65
	 */
66
	uri: string
67
	/**
68
	 * ID associated with desired company that will receive the webhook notifications. NOTE: If no Company ID is provided in the POST, then all companies linked to the app will receive notifications.
69
	 */
70
	companyId?: string
71
	/**
72
	 * Required Authentication object is going to be different based on each authType. <br />* NO_AUTH doesn't have any other fields in authentication object <br />* BASIC_AUTH needs 2 fields: username and password <br />* APIKEY requires the field: apiKey <br />* OAUTH2 requires 5 fields:  tokenUrl, clientId, clientSecret, grantType, contentType <br />* OAUTH2_BASIC requires 5 fields:  tokenUrl, clientId, clientSecret, grantType, contentType
73
	 */
74
	authentication: {
75
		/**
76
		 * The authorization method used to authorize callers to your webhook url.
77
		 */
78
		authType: 'NO_AUTH' | 'BASIC_AUTH' | 'API_KEY' | 'OAUTH2' | 'OAUTH2_BASIC'
79
		/**
80
		 * The API key required for API_KEY authorization.
81
		 */
82
		apiKey?: string
83
		/**
84
		 * The username required for BASIC_AUTH authorization.
85
		 */
86
		username?: string
87
		/**
88
		 * The password required for BASIC_AUTH authorization.
89
		 */
90
		password?: string
91
		/**
92
		 * The URL used to obtain an access token for OAUTH2 or OAUTH2_BASIC authorization.
93
		 */
94
		tokenUrl?: string
95
		/**
96
		 * The client ID required by OAUTH2 and OAUTH2_BASIC authorization.
97
		 */
98
		clientId?: string
99
		/**
100
		 * The client secret required by OAUTH2 and OAUTH2_BASIC authorization.
101
		 */
102
		clientSecret?: string
103
		/**
104
		 * The grant type used to acquire an access token by OAUTH2 and OAUTH2_BASIC authorization.
105
		 */
106
		grantType?: string
107
		/**
108
		 * The content type to use in the token request for OAUTH2 and OAUTH2_BASIC authorization.
109
		 */
110
		contentType?: string
111
		[k: string]: unknown
112
	}
113
	/**
114
	 * List of available domains to register to. Refer to webhook documentation https://developer.paychex.com/documentation#operation/domains.
115
	 */
116
	domains: string[]
117
	links?: {
118
		rel?: string
119
		href?: string
120
		hreflang?: string
121
		media?: string
122
		title?: string
123
		type?: string
124
		deprecation?: string
125
		profile?: string
126
		name?: string
127
		[k: string]: unknown
128
	}[]
129
	[k: string]: unknown
130
}
131