X Enterprises
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:

PropertyTypeDescription
idstringList ID.
namestringList display name.
contact_countnumberNumber 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

AI Context

package: "@xenterprises/fastify-xemail"
method: fastify.xEmail.listsGet()
use-when: Get all SendGrid Marketing contact lists
returns: Promise<Array<{ id, name, contactCount }>>
Copyright © 2026