Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sendgrid-send

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sendgrid-send

Generate JSON body for the SendGrid mail API

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

SendGrid Send

You don't need a library to use a REST API. Just create a valid JSON body, and make a POST request to SendGrid.

Features

  1. TypeScript support - be confident about your JSON.
  2. ESM and CJS - choose either import or require.
  3. Use the Fetch API or any HTTP library1 of your choice.
  4. Literally 0kb - import a single-line JavaScript function.
export const generateSgSendBody = (requestBody) => requestBody;

The requestBody is typed according to the SendGrid API documentation.

Usage

Please reference the SendGrid API documentation for all supported values.

With Fetch API

The generateSgSendRequest function returns a Request object which can be used with the Fetch API. The URL, method, and HTTP headers are all set.

import { generateSgSendRequest } from 'sendgrid-send';

await fetch(
  generateSgSendRequest(
    {
      from: { email: 'sender@doamin.com' },
      personalizations: [{ to: [{ email: 'receiver@domain.com' }] }],
      subject: 'SendGrid and the Fetch API is awesome.',
      content: [{ type: 'text/plain', value: 'Best of both worlds.' }],
      // ...
    },
    '<<YOUR_API_KEY_HERE>>'
  )
);

Generate Body

npm i sendgrid-send
import { generateSgSendBody } from 'sendgrid-send';
// const { generateSgSendBody } = require('sendgrid-send');

const body = generateSgSendBody({
  from: { email: 'sender@doamin.com' },
  // ...
});

// Make a HTTP request using the method of your choice.
// e.g. node-fetch, cross-fetch, axios, got, undici

await fetch('https://api.sendgrid.com/v3/mail/send', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <<YOUR_API_KEY_HERE>>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(body),
});

Import Type Only

npm i sendgrid-send -D
import type { SendGridRequestBody } from 'sendgrid-send';

const body = {
  from: { email: 'sender@doamin.com' },
  // ...
} satisfies SendGridRequestBody;

Notes

JavaScript + VS Code

Create a jsconfig.json file and enable checkJs.

{
  "compilerOptions": {
    "checkJs": true
  }
}

Footnotes

  1. @sendgrid/mail uses Axios - @sendgrid/client depends on it.

Keywords

FAQs

Package last updated on 05 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc