Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to get a session using the Shopify Ruby API?

How to get a session using the Shopify Ruby API?

caffeinesync
Shopify Partner
7 0 1

I am trying to make my Ruby on Rails do a simple request to my shopify products. 

The documentations is not very clear on how to do it. 

Currently Im trying to make a: 

 
  ShopifyAPI::Context.setup(
      api_key: ENV.fetch('SHOPIFY_ACCESS_CLIENT_ID'),
      api_secret_key: ENV.fetch('SHOPIFY_ACCESS_CLIENT_SECRET'),
      host_name: ENV.fetch('SHOPIFY_SHOP_NAME'),
      scope: 'read_orders,read_products,etc',
      is_embedded: false, # Set to true if you are building an embedded app
      is_private: false, # Set to true if you are building a private app
      api_version: '2023-04' # The version of the API you would like to use
    )
 
 
What is the next step? I didnt understand well how to get the session. I just want to fetch my data. 

Sincerely
Replies 4 (4)

SBD_
Shopify Staff
1831 273 423

Hey @caffeinesync 

 

Here are some examples: REST / GraphQL.

Scott | Developer Advocate @ Shopify 

caffeinesync
Shopify Partner
7 0 1

Thanks for the reply. I had seen this link before. 

The thing is, theres a "jump" between the 

 ShopifyAPI::Context.setup


and the

# Create a new client.
client = ShopifyAPI::Clients::Rest::Admin.new(session: session)

 How can I fetch my session object to make the REST call? Its unclear for me and I cant find in any documentation. 

SBD_
Shopify Staff
1831 273 423

Ah I see - I had the same confusion with Private/Custom apps.

 

There's a thread here with some snippets/workarounds.

Scott | Developer Advocate @ Shopify 

caffeinesync
Shopify Partner
7 0 1

This is my code: 

 

ShopifyAPI::Context.setup(
api_key: ENV.fetch('SHOPIFY_API_KEY'),
api_secret_key: ENV.fetch('SHOPIFY_API_SECRET'),
host_name: ENV.fetch('SHOPIFY_SHOP_NAME'),
scope: 'read_orders,read_products,etc',
is_embedded: true, # Set to true if you are building an embedded app
is_private: false, # Set to true if you are building a private app
api_version: '2023-07' # The version of the API you would like to use
)

session = ShopifyAPI::Auth::Session.new(shop: ENV.fetch('SHOPIFY_SHOP_NAME'),
access_token: ENV.fetch('SHOPIFY_API_SECRET'))
puts session.inspect
client = ShopifyAPI::Clients::Rest::Admin.new(session:)
client.get(path: 'orders',
query: { created_at_min: '2023-05-01T00:59:59-08:00', created_at_max: '2023-07-18T23:59:59-08:00', status: 'any',
limit: 250 })

 

 
 
I got my envs from here: 
 
Screenshot 2023-08-10 at 10.22.59.png

 

Still getting an error: 


 

 

  ShopifyAPI::Errors::HttpResponseError:
       {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)","error_reference":"If you report this error, please include this id: 19897226-4b10-4884-a05f-c81768723e3e."}

 

 

 

C'Mon. Its a simple task here. It was not supposed to be as hard to do a simple fetch on the Rest API