How to get a session using the Shopify Ruby API?

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

Hey @caffeinesync

Here are some examples: REST / GraphQL.

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.

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

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

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:

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

1 Like