adding an api key for httparty in ruby on rails

1.9k views Asked by At

I am trying to use the adafruit api to access my data.

Currently when I try and access I get the following message:

{"error":"not found - API documentation can be found at https://io.adafruit.com/api/docs"}

I think this is because I haven't added the key (as I get the same message if you don't use a key on the adafruit example page) but I don't know where to add it.

Currently, I just have this in my controller:

def index

   @tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data')

end

Thanks in advance x

2

There are 2 answers

1
kajal ojha On

Please change your code with

def index

   @tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data?key_id=your_key_id')

end
0
abdollar On

According to the documentation at https://learn.adafruit.com/adafruit-io/browser the parameter for the key is X-AIO-Key. From what I can tell you can use a query param or header so the following combinations should work, in theory, I personally haven't tried them.

@tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data', :headers => { 'content-type': 'application/json', 'X-AIO-Key': 'yourkey' })

@tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data', {:headers => { 'content-type': 'application/json'}, :query => {'X-AIO-Key': 'yourkey' }})

There is also a ruby client library that uses the v1 api at https://github.com/adafruit/io-client-ruby. There is a pull request for v2 that hasn't been merged yet but that library uses faraday instead of httparty.