fastify-xemail
lists-get
Get all SendGrid Marketing contact lists.
getLists
Retrieve all contact lists from SendGrid Marketing. Returns the result array from the SendGrid API, or an empty array if none exist.
Signature
fastify.xEmail.getLists(): Promise<object[]>
Params
None.
Returns
Promise<object[]> — Array of SendGrid list objects. Each object typically contains:
| Property | Type | Description |
|---|---|---|
id | string | List ID. |
name | string | List display name. |
contact_count | number | Number of contacts in the list. |
Returns an empty array if no lists exist.
Throws
[xEmail] Failed to get lists: <reason>— SendGrid API error.
Examples
Basic — list all available lists
fastify.get("/admin/email-lists", async () => {
return fastify.xEmail.getLists();
});
Realistic — find a list by name or create it
async function getOrCreateList(name) {
const lists = await fastify.xEmail.getLists();
const existing = lists.find((l) => l.name === name);
if (existing) return existing;
const { list } = await fastify.xEmail.createList(name);
return list;
}
fastify.post("/newsletter/subscribe", async (request) => {
const list = await getOrCreateList("Newsletter");
await fastify.xEmail.addContact(request.body.email, {}, [list.id]);
return { subscribed: true };
});
See Also
- lists-create — Create a new list
- lists-delete — Delete a list by ID
AI Context
package: "@xenterprises/fastify-xemail"
method: fastify.xEmail.listsGet()
use-when: Get all SendGrid Marketing contact lists
returns: Promise<Array<{ id, name, contactCount }>>
