0

Update a Webhook

by
Published Apr 8, 2025

Updates an existing Webhook.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Update a Webhook
7
 * Updates an existing Webhook.
8
 */
9
export async function main(
10
  auth: Persona,
11
  webhook_id: string,
12
  body: {
13
    data?: {
14
      attributes?: {
15
        url?: string;
16
        "enabled-events"?:
17
          | "*"
18
          | "account.created"
19
          | "account.redacted"
20
          | "account.archived"
21
          | "account.restored"
22
          | "account.consolidated"
23
          | "account.tag-added"
24
          | "account.tag-removed"
25
          | "case.created"
26
          | "case.assigned"
27
          | "case.resolved"
28
          | "case.reopened"
29
          | "case.updated"
30
          | "document.created"
31
          | "document.submitted"
32
          | "document.processed"
33
          | "document.errored"
34
          | "inquiry.created"
35
          | "inquiry.expired"
36
          | "inquiry.completed"
37
          | "inquiry.failed"
38
          | "inquiry.marked-for-review"
39
          | "inquiry.approved"
40
          | "inquiry.declined"
41
          | "inquiry.transitioned"
42
          | "inquiry-session.started"
43
          | "inquiry-session.expired"
44
          | "inquiry-session.canceled"
45
          | "report/address-lookup.ready"
46
          | "report/address-lookup.errored"
47
          | "report/adverse-media.matched"
48
          | "report/adverse-media.ready"
49
          | "report/adverse-media.errored"
50
          | "report/business-adverse-media.matched"
51
          | "report/business-adverse-media.ready"
52
          | "report/business-adverse-media.errored"
53
          | "report/business-watchlist.ready"
54
          | "report/business-watchlist.matched"
55
          | "report/business-watchlist.errored"
56
          | "report/email-address.ready"
57
          | "report/email-address.errored"
58
          | "report/phone-number.ready"
59
          | "report/phone-number.errored"
60
          | "report/profile.ready"
61
          | "report/profile.errored"
62
          | "report/politically-exposed-person.matched"
63
          | "report/politically-exposed-person.ready"
64
          | "report/politically-exposed-person.errored"
65
          | "report/watchlist.matched"
66
          | "report/watchlist.ready"
67
          | "report/watchlist.errored"
68
          | "selfie.created"
69
          | "selfie.submitted"
70
          | "selfie.processed"
71
          | "selfie.errored"
72
          | "transaction.created"
73
          | "transaction.labeled"
74
          | "transaction.redacted"
75
          | "transaction.status-updated"
76
          | "verification.created"
77
          | "verification.submitted"
78
          | "verification.passed"
79
          | "verification.failed"
80
          | "verification.requires-retry"
81
          | "verification.canceled"[];
82
        "api-version"?:
83
          | "2023-01-05"
84
          | "2022-09-01"
85
          | "2021-08-18"
86
          | "2021-07-05"
87
          | "2021-02-21"
88
          | "2020-05-18";
89
        "api-key-inflection"?: "camel" | "kebab" | "snake";
90
        "api-attributes-blocklist"?: string[];
91
        "file-access-token-expires-in"?: number;
92
        "payload-filter"?: { data?: {} };
93
        "custom-http-headers"?: {
94
          Authorization?: string;
95
          "Calling-Application"?: string;
96
          "CF-Access-Client-Id"?: string;
97
          "CF-Access-Client-Secret"?: string;
98
          "X-API-Key"?: string;
99
        };
100
      };
101
    };
102
  },
103
  include?: string,
104
  fields?: string,
105
  Key_Inflection?: string,
106
  Idempotency_Key?: string,
107
  Persona_Version?: string,
108
) {
109
  const url = new URL(
110
    `https://api.withpersona.com/api/v1/webhooks/${webhook_id}`,
111
  );
112
  for (const [k, v] of [
113
    ["include", include],
114
    ["fields", fields],
115
  ]) {
116
    if (v !== undefined && v !== "" && k !== undefined) {
117
      url.searchParams.append(k, v);
118
    }
119
  }
120
  const headers: Record<string, string> = {
121
    Authorization: `Bearer ${auth.apiKey}`,
122
    "Content-Type": "application/json",
123
  };
124
  if (Key_Inflection) {
125
    headers["Key-Inflection"] = Key_Inflection;
126
  }
127
  if (Idempotency_Key) {
128
    headers["Idempotency-Key"] = Idempotency_Key;
129
  }
130
  if (Persona_Version) {
131
    headers["Persona-Version"] = Persona_Version;
132
  }
133
  const response = await fetch(url, {
134
    method: "PATCH",
135
    headers,
136
    body: JSON.stringify(body),
137
  });
138
  if (!response.ok) {
139
    const text = await response.text();
140
    throw new Error(`${response.status} ${text}`);
141
  }
142
  return await response.json();
143
}
144