Closed CaioFML closed 1 year ago
Hey @CaioFML -
Are you using the ShopifyApp Gem? If so, you can follow these instructions on how to use our controller concerns to automatically setup the session behind the scenes for you.
If you're just using the ShopifyAPI gem, you can construct and use the session from your Shopify token like this:
session = ShopifyAPI::Auth::Session.new(
shop: "#{your_shop_name}.myshopify.com"
access_token: "token_for_the_shop"
)
There are 2 ways you can use the session when making API calls:
Passing session object into each client request
def make_api_request(shop)
# 1. create session object
session = ShopifyAPI::Auth::Session.new(
shop: "#{your_shop_name}.myshopify.com"
access_token: "token"
)
# 2a. Create API client with the session information
# session must be type `ShopifyAPI::Auth::Session`
graphql_client = ShopifyAPI::Clients::Graphql::Admin.new(session: session)
response = graphql_client.query(query: MY_API_QUERY)
# 2b. REST example
product_count = ShopifyAPI::Product.count(session: session)
...
end
Setting active_session in ShopifyAPI::Context
#### Configuration
def configure_app
# This method is called before making authenticated API calls
session = ShopifyAPI::Auth::Session.new(
shop: "#{your_shop_name}.myshopify.com"
access_token: "the_token_for_your_custom_app_found_in_admin"
)
# Activate session to be used in all API calls
# session must be type `ShopifyAPI::Auth::Session`
ShopifyAPI::Context.activate_session(session)
end
def make_api_request
ShopifyAPI::Context.active_session
when making API callsgraphql_client = ShopifyAPI::Clients::Graphql::Admin.new
... end
`api_version` is now set as a part of [`ShopifyAPI::Context.setup`](https://github.com/Shopify/shopify-api-ruby/blob/main/README.md#setup-shopify-context). REST Resource will be loaded to the api version specified in the setup. If you're using GraphQL admin or REST Admin clients, they can be specified explicitly as well.
We recently improved our documentation, please take a look to see if it contains more useful information to you:
- [Making GraphQL calls](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/graphql.md)
- [Making REST calls](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/rest.md)
Let me know if you have more questions!
Hello, @zzooeeyy!
Thanks for the answer, the ShopifyAPI::Auth::Session
worked correctly, maybe this need to be added to the docs, I could not find it anywhere
yay! We recently improved some of our docs, it did get added in parts to some of the docs! I'll close this ticket now that it has been resolved.
Issue summary
shopify_api
version: 13.0I'm updating the shopify api and app gems to the latest from an older version where I was using this flow to activate a session and use the shopify_api interface to make requests to the rest api:
But with the latest config, it does not work, or even recognize as a method what is being documented in the shopify docs to use:
Basically I was able to use only the shopify token, domain and api_version to make the requests easily in the older version, is there something that I'm missing right now to initialize this
session
for each shop installed my app?I could do it using
shop.with_shopify_session do |session|
, but seems not right