0

Create a Webhook

by
Published Apr 8, 2025

Creates a new webhook with response defaults.

Script persona Verified

The script

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