/ sendgrid

Communicating with Sendgrid V3 API

Sendgrid provides email service which we can use for sending emails from our app and we can even track every details of
any email sent through our Sendgrid account like whether it is delivered, opened, dropped and details of unsubscribed email
addresses etc.

There are also API endpoints provided by Sendgrid to access user subscribe/unsubscribe related data and email event data
with respect to emails sent through your sendgrid account for which documentation link is provided at bottom.

To understand this lets go through simple example to fetch sendgrid subscription groups data from your sendgrid account -

  • Generate API key in your Sendgrid account and store it in your app based on different environments for which you use that Sendgrid account. While generating API key for your account you can set access permission as per your requirement for that API key. For example I just set Read permissions for Suppression Management Service for my API key as I will be just reading suppression groups data from my account. You can any time change permissions for you API key once it is generated.

sendgrid_v3_api_config.yml

all: &ALL
url: "https://api.sendgrid.com/v3"
development:
<<: *ALL
api_key: "YOUR_API_KEY_FOR_DEVELOPMENT_SENDGRID"
staging:
<<: *ALL
api_key: "YOUR_API_KEY_FOR_STAGING_SENDGRID"
production:
<<: *ALL
api_key: "YOUR_API_KEY_FOR_PROD_SENDGRID"

  • Request to any available Sendgrid V3 API endpoints by using API key in following way -

Example curl request to send email -

  curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"personalizations": [{"to": [{"email": "your.email@example.com"}]}],
  "from": {"email":     "example@example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'

Rails service using Net HTTP library to access Sendgrid suppression management endpoint to get suppression groups -
Here I have written service to make make get request only, you can make any other request as well using Net HTTP library and modify following snippet as per your need.

class SendgridApiService
  SENDGRID_CONFIG = YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config', 'sendgrid_v3_api_config.yml'))).result)[Rails.env]


  attr_accessor :api_key, :endpoint_url

  def get_response
    response = request_object.request(request_with_auth_header)
    return unless response.is_a?(Net::HTTPSuccess)
    response.body and JSON.parse(response.body)
  end

  def initialize(api_endpoint)
    self.api_key = SENDGRID_CONFIG['api_key']
    self.endpoint_url = SENDGRID_CONFIG['url'] + api_endpoint
  end

  def new_uri
    URI.parse(endpoint_url)
  end

  def request_object
    http_obj = Net::HTTP.new(new_uri.host, new_uri.port)
    http_obj.use_ssl = true
    http_obj.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http_obj
  end

  def request_with_auth_header
    request = Net::HTTP::Get.new(new_uri.path)
    request['Authorization'] = "Bearer #{api_key}"
    request['content-type']  = "application/json"
    request
  end
end

And now you can pass any endpoint available for get request to above service and read from that endpoint.
For example in above case to fetch subscription groups data from sendgrid asm groups endpoint -

SendgridApiService.new("/asm/groups").get_response

Sendgrid V3 API documentation

Tushar Titame

I am software enthusiast. I like to work on web frameworks, services and trending things in JS world. As a hobby I love visiting beautiful places and capturing them through eyes...

Read More