diff --git a/docs/use-cases/data-residency-set-hostname.md b/docs/use-cases/data-residency-set-hostname.md index f7ace11b..0fc0927d 100644 --- a/docs/use-cases/data-residency-set-hostname.md +++ b/docs/use-cases/data-residency-set-hostname.md @@ -1,8 +1,8 @@ -# Choosing a hostname to send messages to +# Choosing a data-residency to send messages to Use the `setDataResidency` setter to specify which host to send to: -Send to EU (hostname: `https://api.eu.sendgrid.com/`) +Send to EU (data-residency: `https://api.eu.sendgrid.com/`) ```js const sgMail = require('@sendgrid/mail'); sgMail.setDataResidency('eu'); @@ -16,7 +16,7 @@ const msg = { sgMail.send(msg); ``` Send to Global region, this is also the default host, if the setter is not used -(hostname: `https://api.sendgrid.com/`) +(data-residency: `https://api.sendgrid.com/`) ```js const sgMail = require('@sendgrid/mail'); sgMail.setDataResidency('global'); @@ -32,7 +32,6 @@ 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/ +1. 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 data-residency is https://api.sendgrid.com/ 3. The valid values for `region` in `client.setDataResidency(region)` are only `eu` and `global`. Case-sensitive. diff --git a/packages/client/src/classes/client.js b/packages/client/src/classes/client.js index c0c051fe..22357c0b 100644 --- a/packages/client/src/classes/client.js +++ b/packages/client/src/classes/client.js @@ -14,7 +14,7 @@ const { const API_KEY_PREFIX = 'SG.'; const SENDGRID_BASE_URL = 'https://api.sendgrid.com/'; const TWILIO_BASE_URL = 'https://email.twilio.com/'; - +const SENDGRID_REGION = 'global'; // Initialize the allowed regions and their corresponding hosts const REGION_HOST_MAP = { eu: 'https://api.eu.sendgrid.com/', @@ -24,7 +24,7 @@ class Client { constructor() { this.auth = ''; this.impersonateSubuser = ''; - this.sendgrid_region = ''; + this.sendgrid_region = SENDGRID_REGION; this.defaultHeaders = { Accept: 'application/json', @@ -44,10 +44,8 @@ class Client { setApiKey(apiKey) { this.auth = 'Bearer ' + apiKey; - // this means that region was never set before - if (this.sendgrid_region == '') { - this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL); - } + this.setDefaultRequest('baseUrl', REGION_HOST_MAP[this.sendgrid_region]); + if (!this.isValidApiKey(apiKey)) { console.warn(`API key does not start with "${API_KEY_PREFIX}".`); }