Rails - How to add contacts to SendGrid marketing campaigns via API

回眸只為那壹抹淺笑 提交于 2019-12-02 06:29:23

I ended up figuring it out. For future reference, here's the code that worked...

require 'rest_client'
api_key = 'YOUR_API_KEY'
headers = {'Authorization' => "Bearer #{api_key}"}
data = {:email => 'email@website.com'}
response = RestClient.post 'https://api.sendgrid.com/v3/contactdb/recipients', [data].to_json, headers

To add marketing contacts to your SendGrid account via the API, see the documentation at https://sendgrid.api-docs.io/v3.0/contacts-api-recipients/add-recipients

You can see sample code in the "code generation" section of the page.

require 'uri'
require 'net/http'

url = URI("https://api.sendgrid.com/v3/contactdb/recipients")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["authorization"] = 'Bearer <<YOUR_API_KEY>>'
request["content-type"] = 'application/json'
request.body = "[{\"email\":\"example@example.com\",\"first_name\":\"\",\"last_name\":\"User\",\"age\":25},{\"email\":\"example2@example.com\",\"first_name\":\"Example\",\"last_name\":\"User\",\"age\":25}]"

response = http.request(request)
puts response.read_body
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!