fastify-xemail
lists-create
Create a new SendGrid Marketing contact list.
createList
Create a new contact list in SendGrid Marketing. The returned list.id can be passed to addContact to subscribe contacts to the new list.
Signature
fastify.xEmail.createList(name: string): Promise<{ success: boolean; list: object }>
Params
| Name | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Display name for the new contact list. |
Returns
Promise<{ success: boolean; list: object }>
| Property | Type | Description |
|---|---|---|
success | boolean | Always true on resolved promise. |
list | object | Full SendGrid list object, including id and name. |
Throws
[xEmail] 'name' (string) is required for createList().[xEmail] Failed to create list: <reason>— SendGrid API error.
Examples
Basic — create a list
fastify.post("/admin/lists", async (request) => {
const { name } = request.body;
const result = await fastify.xEmail.createList(name);
return { listId: result.list.id, name: result.list.name };
});
Realistic — create a list and subscribe the first contact
fastify.post("/campaigns/setup", async (request) => {
const { listName, seedEmail } = request.body;
const { list } = await fastify.xEmail.createList(listName);
await fastify.xEmail.addContact(seedEmail, {}, [list.id]);
await db.campaigns.create({ listId: list.id, listName });
return { listId: list.id, subscribed: seedEmail };
});
See Also
- lists-get — Retrieve all existing lists
- lists-delete — Delete a list by ID
- contacts-add — Add a contact to this list using its ID
AI Context
package: "@xenterprises/fastify-xemail"
method: fastify.xEmail.listsCreate(name)
use-when: Create a new SendGrid Marketing contact list
params: name (string)
returns: Promise<{ id: string, name: string }>
