Skip to content
On this page
netzo/apis/sendgrid

SendGrid

SendGrid is a cloud-based email service that provides reliable transactional email delivery, scalability, and real-time analytics along with flexible APIs that make custom integration easy.

  • labels: email, email-marketing, email-automation, email-delivery
  • authentication: apiKey

Usage

ts
import { sendgrid } from 'https://deno.land/x/netzo/apis/sendgrid/mod.ts'

const { api } = sendgrid({
  apiKey: Deno.env.get('SENDGRID_API_KEY')
})
import { sendgrid } from 'https://deno.land/x/netzo/apis/sendgrid/mod.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 HTTP 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. Refer to the type definitions for all exported types to pass to the api client for typed responses.

Find lists

Find all your contact lists.

ts
import type { Lists, QueryLists } from 'netzo/apis/sendgrid/types.ts'

const query: QueryLists = {}
const result = await api.marketing.lists.get<Lists>(query)
const resultData = result.result
import type { Lists, QueryLists } from 'netzo/apis/sendgrid/types.ts'

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

Get list

Get a list by id.

ts
import type { List, QueryList } from 'netzo/apis/sendgrid/types.ts'

const query: QueryList = {}
const resultData = await api.marketing.lists[LIST_ID].get<List>(query)
import type { List, QueryList } from 'netzo/apis/sendgrid/types.ts'

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

Update list name

Update the name of a list.

ts
import type { DataUpdateList, UpdateListResult } from 'netzo/apis/sendgrid/types.ts'

const data: DataUpdateList = { name: 'Updated list name' }
const resultData = await api.marketing.lists[LIST_ID].patch<UpdateListResult>(data)
import type { DataUpdateList, UpdateListResult } from 'netzo/apis/sendgrid/types.ts'

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.

ts
import type { AddContactsResult, DataAddContacts } from 'netzo/apis/sendgrid/types.ts'

const data: DataAddContacts = {
  list_ids: [LIST_ID1, LIST_ID2],
  contacts: [{ email: '[email protected]' }]
}
const resultData = await api.marketing.contacts.put<AddContactsResult>(data)
import type { AddContactsResult, DataAddContacts } from 'netzo/apis/sendgrid/types.ts'

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.

ts
import type { DataSendEmail } from 'netzo/apis/sendgrid/types.ts'

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)
import type { DataSendEmail } from 'netzo/apis/sendgrid/types.ts'

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