Skip to content

Managing Contact Methods

Create, list, and delete contact methods for users.

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}`);
const email = await client.contactMethod.create({
userId: "usr_alice",
transport: "email",
value: "alice@example.com",
});
const sms = await client.contactMethod.create({
userId: "usr_alice",
transport: "sms",
value: "+14155551234", // E.164 format recommended
});
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}`);
}
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}`);
});
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}`);
const result = await client.contactMethod.delete("cm_abc123", {
userId: "usr_alice",
});
console.log("Deleted successfully:", result.success);
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);
}
type CreateContactMethodInput = {
userId: string; // User ID this contact method belongs to
transport: "email" | "sms"; // Communication channel type
value: string; // Email address or phone number
};
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
};
type ListContactMethodsParams = {
userId: string; // User ID to list contact methods for
};
type DeleteContactMethodParams = {
userId: string; // User ID (for authorization)
};
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`);

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",
});
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}`);
}
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,
});
}

No one receiving notifications? See Troubleshooting - No One Is Notified.