-
Notifications
You must be signed in to change notification settings - Fork 783
feat: Add data residency for eu and global regions #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
91b4cb6
50a8691
2f7c9ea
a246759
1c79ce7
a21b169
b356716
8bfe5cd
3f90c9f
d0cc9e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Choosing a hostname to send messages to | ||
|
|
||
| Use the `setDataResidency` setter to specify which host to send to: | ||
|
|
||
| Send to EU (hostname: `https://api.eu.sendgrid.com/`) | ||
| ```js | ||
| const sgMail = require('@sendgrid/mail'); | ||
| sgMail.setDataResidency('eu'); | ||
| const msg = { | ||
| to: 'recipient@example.org', | ||
| from: 'sender@example.org', | ||
| subject: 'Hello world', | ||
| text: 'Hello plain world!', | ||
| html: '<p>Hello HTML world!</p>', | ||
| }; | ||
| sgMail.send(msg); | ||
| ``` | ||
| Send to Global region, this is also the default host, if the setter is not used | ||
| (hostname: `https://api.sendgrid.com/`) | ||
| ```js | ||
| const sgMail = require('@sendgrid/mail'); | ||
| sgMail.setDataResidency('global'); | ||
| const msg = { | ||
| to: 'recipient@example.org', | ||
| from: 'sender@example.org', | ||
| subject: 'Hello world', | ||
| text: 'Hello plain world!', | ||
| html: '<p>Hello HTML world!</p>', | ||
| }; | ||
| sgMail.send(msg); | ||
| ``` | ||
|
|
||
| ## Limitations | ||
|
|
||
| 1. Setting the API Key (via `client.setApiKey()`) or Twilio Authentication (via `client.setTwilioEmailAuth()`) will override the hostname to default value. Use the setter call after this set-up. | ||
|
||
| 2. Emails can only be sent to two hosts for now; 'eu' (https://api.eu.sendgrid.com/) and 'global' (https://api.eu.sendgrid.com/) | ||
| 2. The default hostname is https://api.sendgrid.com/ | ||
| 3. The valid values for `region` in `client.setDataResidency(region)` are only `eu` and `global`. Case-sensitive. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,10 +15,16 @@ const API_KEY_PREFIX = 'SG.'; | |
| const SENDGRID_BASE_URL = 'https://api.sendgrid.com/'; | ||
| const TWILIO_BASE_URL = 'https://email.twilio.com/'; | ||
|
|
||
| // Initialize the allowed regions and their corresponding hosts | ||
| const REGION_HOST_MAP = { | ||
| eu: 'https://api.eu.sendgrid.com/', | ||
| global: 'https://api.sendgrid.com/', | ||
| }; | ||
| class Client { | ||
| constructor() { | ||
| this.auth = ''; | ||
| this.impersonateSubuser = ''; | ||
| this.sendgrid_region = ''; | ||
shrutiburman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| this.defaultHeaders = { | ||
| Accept: 'application/json', | ||
|
|
@@ -38,8 +44,10 @@ class Client { | |
|
|
||
| setApiKey(apiKey) { | ||
| this.auth = 'Bearer ' + apiKey; | ||
| this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL); | ||
|
|
||
| // this means that region was never set before | ||
| if (this.sendgrid_region == '') { | ||
| this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL); | ||
| } | ||
|
||
| if (!this.isValidApiKey(apiKey)) { | ||
| console.warn(`API key does not start with "${API_KEY_PREFIX}".`); | ||
| } | ||
|
|
@@ -94,6 +102,21 @@ class Client { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Global is the default residency (or region) | ||
| * Global region means the message will be sent through https://api.sendgrid.com | ||
| * EU region means the message will be sent through https://api.eu.sendgrid.com | ||
| **/ | ||
| setDataResidency(region) { | ||
| if (!REGION_HOST_MAP.hasOwnProperty(region)) { | ||
| console.warn('Region can only be "global" or "eu".'); | ||
| } else { | ||
| this.sendgrid_region = region; | ||
| this.setDefaultRequest('baseUrl', REGION_HOST_MAP[region]); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| createHeaders(data) { | ||
| // Merge data with default headers. | ||
| const headers = mergeData(this.defaultHeaders, data); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.