0

I've set my environment like so:

echo "export SENDGRID_API_KEY='xxxxxxxxx'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

Sendgrid gem is installed.

Code I try to run:

require 'sendgrid-ruby'
include SendGrid
require 'json'

def hello_world
  from = Email.new(email: 'test@example.com')
  to = Email.new(email: 'test@example.com')
  subject = 'Sending with SendGrid is Fun'
  content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
  mail = Mail.new(from, subject, to, content)

  #previous version
  #sg = SendGrid::API.new(api_key: ENV['xxxxxxx'])

  #current version
  sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
  response = sg.client.mail._('send').post(request_body: mail.to_json)
  puts response.status_code
  puts response.body
  puts response.headers
end

hello_world

But I get the following error:

no implicit conversion of nil into String

Inside this file:

/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in

I cant figure out what goes wrong here....

Full error:

/Users/pimzonneveld/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in `+': no implicit conversion of nil into String (TypeError)
    from /Users/pimzonneveld/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in `initialize'
    from app/helpers/mail.rb:12:in `new'
    from app/helpers/mail.rb:12:in `hello_world'
    from app/helpers/mail.rb:19:in `<main>'
Pimmesz
  • 335
  • 2
  • 8
  • 29

2 Answers2

2

You are confusing the value of the env variable (xxxxxxxxx) with it's name (SENDGRID_API_KEY).

When setting the API key in your code, use

  sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])

instead.

mikezter
  • 2,393
  • 1
  • 17
  • 24
  • I still have a strange issue. The code above was put in helpers/mail.rb and when executed from the commandline it works. However when I put it in a controller and try to execute I still get the `no implicit conversion of nil into String` error. Do you perhaps know what could cause this? – Pimmesz Apr 18 '18 at 09:30
  • 1
    you probably didn't source your env-file before starting the rails server (in the same shell). – mikezter Apr 18 '18 at 10:03
  • After fully restarting the server, restarting sublime (.env file still there with correct info) and after sourcing the env-file again just to be sure I still get the error. I really strange that the mail.rb in app/helper works but it breaks in another controller? – Pimmesz Apr 18 '18 at 10:08
  • 1
    you dont need to restart your editor. how are you starting your server? try this: `SENDGRID_API_KEY=xxxxxxxx rails s` – mikezter Apr 18 '18 at 10:09
  • Thanks that seems to work! But how can I set that API key so I can use it on production as well? Should it be set in application.yml? – Pimmesz Apr 18 '18 at 10:24
  • 1
    Basically you'll have to make sure that the prod-server provides the env-variables as well. But thats a different question. Read about Rails secrets here https://www.engineyard.com/blog/encrypted-rails-secrets-on-rails-5.1 and about environment variables here https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps – mikezter Apr 18 '18 at 10:35
0

Deep is right. /ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in refers to

"Authorization": "Bearer ' + @api_key + '",

so the no implicit conversion of nil into String makes perfect sense.

You should change

sg = SendGrid::API.new(api_key: ENV['xxxxxxx'])

for:

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
Alejandro Marti
  • 113
  • 1
  • 6
  • I still have a strange issue. The code above was put in helpers/mail.rb and when executed from the commandline it works. However when I put it in a controller and try to execute I still get the `no implicit conversion of nil into String` error. Do you perhaps know what could cause this? – Pimmesz Apr 18 '18 at 09:30