Netzo Logo

sendgrid

SendGrid is a cloud-based email service.

Usage

import { sendgrid } from 'netzo/apis/sendgrid.ts'

const api = sendgrid({
  apiKey: Deno.env.get('SENDGRID_API_KEY')
})

Configuration

The sendgrid factory function expects an object with the following, and returns an object with an API client api.

ParamTypeDefaultDescription
apiKeystringDeno.env.get('SENDGRID_API_KEY')the api key to use for authentication
Refer to the API documentation to get the required information.

Examples

The following examples assume you have created an api client instance.

Find lists

Find all your contact lists.

const query: QueryLists = {}
const result = await api.marketing.lists.get<Lists>(query)
const resultData = result.result

Get list

Get a list by id.

const query: QueryList = {}
const resultData = await api.marketing.lists[LIST_ID].get<List>(query)

Update list name

Update the name of a list.

const data: DataUpdateList = { name: 'Updated list name' }
const resultData = await api.marketing.lists[LIST_ID].patch<UpdateListResult>(data)

Add contacts to list

Add contacts to one or multiple lists.

const data: DataAddContacts = {
  list_ids: [LIST_ID1, LIST_ID2],
  contacts: [{ email: '[email protected]' }]
}
const resultData = await api.marketing.contacts.put<AddContactsResult>(data)

Send email

Send an email over the API.

const data: DataSendEmail = {
  personalizations: [
    {
      to: [
        {
          email: '[email protected]',
          name: 'Contact name'
        }
      ]
    }
  ],
  from: {
    email: '[email protected]',
    name: 'Sender name'
  },
  subject: 'New Subject',
  content: [
    {
      type: 'text/html',
      value: '<p>Content of my email</p>'
    }
  ]
}
await api.mail.send.post<void>(data)

References