# Integration

At the moment, the Sendgrid integration consists in:

* Register users subscribed to the newsletter into [Sendgrid's contact list](https://sendgrid.com/solutions/email-marketing/email-list-management/).
* Send notification email on minting.

The emails are stored in the `_Users` table, but the state of the subscription is persisted and consulted directly using Sendgrid API.

# Requirements

* Sendgrid account
* Create an API key (Settings -> API Keys)
* Create a contact list for the newsletter (Marketing -> Contacts)
* Create a dynamic template (Email API -> Dynamic Templates)
  * For mint notification

# Config

|Parameter|Description|
|---|---|
|SENDGRID_API_URL|API URL -> `https://api.sendgrid.com/v3`|
|SENDGRID_API_KEY|API key created in Sendgrid dashboard|
|SENDGRID_SENDER_EMAIL|Authenticated sender email (Settings -> Sender Authentication)|
|SENDGRID_SENDER_NAME|Custom sender name (optional)|
|SENDGRID_NEWSLETTER_LIST_ID|Id. of the list created in Sendgrid. (You can get it entering to the list and taking a look in the URL of the browser) ie: `ee3f7123-8163-4498-b46b-99809a76ef89`|
|SENDGRID_MINT_TEMPLATE_ID|Id. of the dynamic template. (Email API -> Dynamic Templates)|

> Parameters has to be accessible only with a master key. (Active the check on Moralis dashboard parameter form). Especially the **SENDGRID_API_KEY**.

# Endpoints used

* [POST /v3/marketing/contacts/search/emails](https://docs.sendgrid.com/api-reference/contacts/get-contacts-by-emails)
* [PUT /v3/marketing/contacts](https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact)
* [DELETE /v3/marketing/lists/{id}/contacts](https://docs.sendgrid.com/api-reference/lists/remove-contacts-from-a-list)
* [POST /v3/mail/send](https://docs.sendgrid.com/api-reference/mail-send/mail-send)
