Created
February 3, 2025 16:07
-
-
Save ntotten/b5731ce44796b99a8803f63836ad54ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ZuploContext, ZuploRequest, environment } from "@zuplo/runtime"; | |
interface UserEvent { | |
object: "event"; | |
type: "user.created"; | |
data: UserData; | |
} | |
interface UserData { | |
birthday: string; | |
created_at: number; | |
email_addresses: EmailAddress[]; | |
external_accounts: any[]; | |
external_id: string; | |
first_name: string; | |
gender: string; | |
id: string; | |
image_url: string; | |
last_name: string; | |
last_sign_in_at: number; | |
object: "user"; | |
password_enabled: boolean; | |
phone_numbers: any[]; | |
primary_email_address_id: string; | |
primary_phone_number_id: string | null; | |
primary_web3_wallet_id: string | null; | |
private_metadata: Record<string, any>; | |
profile_image_url: string; | |
public_metadata: Record<string, any>; | |
two_factor_enabled: boolean; | |
unsafe_metadata: Record<string, any>; | |
updated_at: number; | |
username: string | null; | |
web3_wallets: any[]; | |
} | |
interface EmailAddress { | |
email_address: string; | |
id: string; | |
linked_to: any[]; | |
object: "email_address"; | |
verification: Verification; | |
} | |
interface Verification { | |
status: "verified" | "unverified"; | |
strategy: string; | |
} | |
export default async function (request: ZuploRequest, context: ZuploContext) { | |
const event: UserEvent = await request.json(); | |
const body = { | |
description: `Consumer for ${event.data.id}`, | |
managers: [event.data.email_addresses[0].email_address], | |
metadata: { | |
// Any metadata here | |
user_id: event.data.id, | |
}, | |
name: `c-${crypto.randomUUID()}`, | |
}; | |
// Create the consumer | |
const response = await fetch( | |
`https://dev.zuplo.com/v1/accounts/${environment.ZUPLO_ACCOUNT_NAME}/key-buckets/${environment.API_KEY_BUCKET}/consumers?with-api-key=true`, | |
{ | |
method: "POST", | |
body: JSON.stringify(body), | |
headers: { | |
Authorization: `Bearer ${environment.ZUPLO_API_KEY}`, | |
"content-type": "application/json", | |
}, | |
}, | |
); | |
const result = await response.json(); | |
if (response.status !== 200) { | |
context.log.error(result); | |
throw new Error("Error creating API consumer"); | |
} | |
return { status: "OK" } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment