|
| 1 | +# Custom Apps |
| 2 | + |
| 3 | +If you have followed instructions on creating [custom apps](https://help.shopify.com/current/manual/apps/app-types/custom-apps), you should be able to access your API token without having to go through the OAuth flow. |
| 4 | + |
| 5 | +You can follow instructions for [initializing the session object](#initializing-the-session-object) to construct the session object to be used in authenticated API calls to your store. There are 2 methods to use the session object to make API calls: |
| 6 | +1. [Passing `session` object into each client request](#passing-session-object-into-each-client-request) |
| 7 | +2. [Setting `active_session` in `ShopifyAPI::Context`](#setting-active-session-in-shopifyapicontext) |
| 8 | + |
| 9 | +## Initializing the Session object |
| 10 | +Following is a basic example to construct a simple Session object. You can see full list of parameters for this object in the [class definition](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/auth/session.rb) |
| 11 | +```ruby |
| 12 | +session = ShopifyAPI::Auth::Session.new( |
| 13 | + shop: "#{your_shop_name}.myshopify.com" |
| 14 | + access_token: "the_token_for_your_custom_app_found_in_admin" |
| 15 | +) |
| 16 | + |
| 17 | +``` |
| 18 | +## Using `Session` to make API calls |
| 19 | + |
| 20 | +### Passing `session` object into each client request |
| 21 | +Example: |
| 22 | +```ruby |
| 23 | +def make_api_request(shop) |
| 24 | + # 1. create session object |
| 25 | + session = ShopifyAPI::Auth::Session.new( |
| 26 | + shop: "#{your_shop_name}.myshopify.com" |
| 27 | + access_token: "the_token_for_your_custom_app_found_in_admin" |
| 28 | + ) |
| 29 | + |
| 30 | + # 2a. Create API client with the session information |
| 31 | + # session must be type `ShopifyAPI::Auth::Session` |
| 32 | + graphql_client = ShopifyAPI::Clients::Graphql::Admin.new(session: session) |
| 33 | + response = graphql_client.query(query: MY_API_QUERY) |
| 34 | + |
| 35 | + # 2b. REST example |
| 36 | + product_count = ShopifyAPI::Product.count(session: session) |
| 37 | + |
| 38 | + ... |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +### Setting `active_session` in `ShopifyAPI::Context` |
| 43 | +Alternatively, if you don't want to keep having to create/retrieve a Session object for a shop, you may set [`ShopifyAPI::Context.active_session`](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/context.rb#L144). |
| 44 | +All of the API client classes will [use the `active_session`](https://github.com/Shopify/shopify-api-ruby/blob/c3bb9d8f8b6053756149a4cf9299e059ec337544/lib/shopify_api/clients/http_client.rb#L13) if the `session` passed in is `nil`. |
| 45 | + |
| 46 | +Example: |
| 47 | +```ruby |
| 48 | +#### Configuration |
| 49 | +def configure_app |
| 50 | + # This method is called before making authenticated API calls |
| 51 | + session = ShopifyAPI::Auth::Session.new( |
| 52 | + shop: "#{your_shop_name}.myshopify.com" |
| 53 | + access_token: "the_token_for_your_custom_app_found_in_admin" |
| 54 | + ) |
| 55 | + |
| 56 | + # Activate session to be used in all API calls |
| 57 | + # session must be type `ShopifyAPI::Auth::Session` |
| 58 | + ShopifyAPI::Context.activate_session(session) |
| 59 | +end |
| 60 | + |
| 61 | +#### Using clients to make authenticated API calls |
| 62 | +def make_api_request |
| 63 | + # 1. Create API client without session information |
| 64 | + # The graphql_client will use `ShopifyAPI::Context.active_session` when making API calls |
| 65 | + graphql_client = ShopifyAPI::Clients::Graphql::Admin.new |
| 66 | + |
| 67 | + # 2. Use API client to make queries |
| 68 | + ... |
| 69 | +end |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +⚠️ See following docs on how to use the API clients: |
| 74 | +- [Make a GraphQL API call](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/graphql.md) |
| 75 | +- [Make a REST API call](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/rest.md) |
0 commit comments