Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

v3 API Ruby Code Example


(information)

Info

We recommend using SendGrid Ruby, our client library, available on GitHub(link takes you to an external page), with full documentation.

(information)

Info

Do you have an API Key(link takes you to an external page) yet? If not, go get one. You're going to need it to integrate!


Using SendGrid's Ruby Library

using-sendgrids-ruby-library page anchor

The following example sends a single email with the Twilio SendGrid Ruby library. Set your API key in the SENDGRID_API_KEY environment variable, then build and send the message.

Send an email with the Twilio SendGrid Ruby library

send-an-email-with-the-twilio-sendgrid-ruby-library page anchor
1
# using SendGrid's Ruby Library
2
# https://github.com/sendgrid/sendgrid-ruby
3
require 'sendgrid-ruby'
4
include SendGrid
5
6
from = SendGrid::Email.new(email: 'test@example.com')
7
to = SendGrid::Email.new(email: 'test@example.com')
8
subject = 'Sending with SendGrid is Fun'
9
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
10
mail = SendGrid::Mail.new(from, subject, to, content)
11
12
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
13
response = sg.client.mail._('send').post(request_body: mail.to_json)
14
puts response.status_code
15
puts response.body
16
puts response.headers