Managing Contact Methods
Create, list, and delete contact methods for users.
Creating a Contact Method
Section titled “Creating a Contact Method”import { BeepsClient } from "@beepsdev/sdk";
const client = new BeepsClient({ apiKey: process.env.BEEPS_API_KEY,});
const emailContact = await client.contactMethod.create({ userId: "usr_alice", transport: "email", value: "alice@example.com",});
console.log(`Created contact method: ${emailContact.id}`);Email Contact Method
Section titled “Email Contact Method”const email = await client.contactMethod.create({ userId: "usr_alice", transport: "email", value: "alice@example.com",});SMS Contact Method
Section titled “SMS Contact Method”const sms = await client.contactMethod.create({ userId: "usr_alice", transport: "sms", value: "+14155551234", // E.164 format recommended});Safe Method (No Exceptions)
Section titled “Safe Method (No Exceptions)”const result = await client.contactMethod.createSafe({ userId: "usr_alice", transport: "email", value: "alice@example.com",});
if (result.error) { console.error("Failed:", result.error.message);} else { console.log(`Created: ${result.data.id}`);}curl -X POST https://api.beeps.dev/v0/contact-methods \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "userId": "usr_alice", "transport": "email", "value": "alice@example.com" }'Response:
{ "contactMethod": { "id": "cm_abc123", "userId": "usr_alice", "transport": "email", "value": "alice@example.com", "verified": false, "createdAt": "2025-01-15T10:30:00Z", "updatedAt": "2025-01-15T10:30:00Z", "deletedAt": null }}Listing Contact Methods
Section titled “Listing Contact Methods”const methods = await client.contactMethod.list({ userId: "usr_alice",});
console.log(`Found ${methods.length} contact methods`);
methods.forEach((method) => { console.log(`- ${method.transport}: ${method.value}`); console.log(` Verified: ${method.verified}`);});Filter by Transport Type
Section titled “Filter by Transport Type”const methods = await client.contactMethod.list({ userId: "usr_alice",});
const emailMethods = methods.filter((m) => m.transport === "email");const smsMethods = methods.filter((m) => m.transport === "sms");
console.log(`Emails: ${emailMethods.length}`);console.log(`SMS: ${smsMethods.length}`);curl -X GET "https://api.beeps.dev/v0/contact-methods?userId=usr_alice" \ -H "Authorization: Bearer YOUR_API_KEY"Response:
{ "contactMethods": [ { "id": "cm_abc123", "userId": "usr_alice", "transport": "email", "value": "alice@example.com", "verified": true, "createdAt": "2025-01-15T10:30:00Z", "updatedAt": "2025-01-15T10:30:00Z", "deletedAt": null }, { "id": "cm_def456", "userId": "usr_alice", "transport": "sms", "value": "+14155551234", "verified": false, "createdAt": "2025-01-15T11:00:00Z", "updatedAt": "2025-01-15T11:00:00Z", "deletedAt": null } ]}Deleting a Contact Method
Section titled “Deleting a Contact Method”const result = await client.contactMethod.delete("cm_abc123", { userId: "usr_alice",});
console.log("Deleted successfully:", result.success);Safe Method
Section titled “Safe Method”const result = await client.contactMethod.deleteSafe("cm_abc123", { userId: "usr_alice",});
if (result.error) { console.error("Failed to delete:", result.error.message);} else { console.log("Deleted:", result.data.success);}curl -X DELETE "https://api.beeps.dev/v0/contact-methods/cm_abc123?userId=usr_alice" \ -H "Authorization: Bearer YOUR_API_KEY"Response:
{ "success": true}Type Definitions
Section titled “Type Definitions”CreateContactMethodInput
Section titled “CreateContactMethodInput”type CreateContactMethodInput = { userId: string; // User ID this contact method belongs to transport: "email" | "sms"; // Communication channel type value: string; // Email address or phone number};ContactMethod
Section titled “ContactMethod”type ContactMethod = { id: string; // Unique contact method identifier userId: string; // User this belongs to transport: string; // "email" or "sms" value: string; // Email address or phone number verified: boolean; // Whether the contact method is verified createdAt: string; // ISO 8601 timestamp updatedAt: string; // ISO 8601 timestamp deletedAt: string | null; // Soft delete timestamp};ListContactMethodsParams
Section titled “ListContactMethodsParams”type ListContactMethodsParams = { userId: string; // User ID to list contact methods for};DeleteContactMethodParams
Section titled “DeleteContactMethodParams”type DeleteContactMethodParams = { userId: string; // User ID (for authorization)};Common Patterns
Section titled “Common Patterns”Setting Up a New User
Section titled “Setting Up a New User”const userId = "usr_alice";
await client.contactMethod.create({ userId, transport: "email", value: "alice@example.com",});
await client.contactMethod.create({ userId, transport: "sms", value: "+14155551234",});
const methods = await client.contactMethod.list({ userId });console.log(`User has ${methods.length} contact methods configured`);Updating Contact Information
Section titled “Updating Contact Information”To update a contact method, delete the old one and create a new one:
const methods = await client.contactMethod.list({ userId: "usr_alice",});
const oldEmail = methods.find( (m) => m.transport === "email" && m.value === "old@example.com");
if (oldEmail) { await client.contactMethod.delete(oldEmail.id, { userId: "usr_alice", });}
await client.contactMethod.create({ userId: "usr_alice", transport: "email", value: "new@example.com",});Bulk User Setup
Section titled “Bulk User Setup”const users = [ { id: "usr_alice", email: "alice@example.com", phone: "+14155551234" }, { id: "usr_bob", email: "bob@example.com", phone: "+14155555678" },];
for (const user of users) { await client.contactMethod.create({ userId: user.id, transport: "email", value: user.email, });
await client.contactMethod.create({ userId: user.id, transport: "sms", value: user.phone, });
console.log(`Configured contact methods for ${user.email}`);}Checking for Existing Contact Methods
Section titled “Checking for Existing Contact Methods”const userId = "usr_alice";const newEmail = "alice@example.com";
const existing = await client.contactMethod.list({ userId });
const alreadyExists = existing.some( (m) => m.transport === "email" && m.value === newEmail);
if (!alreadyExists) { await client.contactMethod.create({ userId, transport: "email", value: newEmail, });}Troubleshooting
Section titled “Troubleshooting”No one receiving notifications? See Troubleshooting - No One Is Notified.