Skip to contentSkip to navigationSkip to topbar
On this page

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
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