Skip to content

Commit 38166be

Browse files
authored
Merge pull request #1221 from Shopify/zoey/custom-apps
Add page to describe how to create sessions for Custom Apps
2 parents 8bb304b + 8dcd2ab commit 38166be

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

docs/usage/custom_apps.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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)

docs/usage/graphql.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Every API request requires a valid
66
[ShopifyAPI::Auth::Session](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/auth/session.rb).
77

88
To instantiate a session, we recommend you either use the `shopify_app` if working in Rails, or refer to our OAuth docs on constructing a session:
9+
- ["Custom Apps"](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/custom_apps.md) - documentation on how to create Session from a custom app API token.
910
- ["Performing OAuth"](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/oauth.md) - documentation on how to create new sessions
1011
- [[ShopifyApp] - "Session"](https://github.com/Shopify/shopify_app/blob/main/docs/shopify_app/sessions.md) - documentation on session handling if you're using the [`ShopifyApp`](https://github.com/Shopify/shopify_app) gem.
1112

docs/usage/rest.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Every API request requires a valid
66
[ShopifyAPI::Auth::Session](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/auth/session.rb).
77

88
To instantiate a session, we recommend you either use the `shopify_app` if working in Rails, or refer to our OAuth docs on constructing a session:
9+
- ["Custom Apps"](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/custom_apps.md) - documentation on how to create Session from a custom app API token.
910
- ["Performing OAuth"](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/oauth.md) - documentation on how to create new sessions
1011
- [[ShopifyApp] - "Session"](https://github.com/Shopify/shopify_app/blob/main/docs/shopify_app/sessions.md) - documentation on session handling if you're using the [`ShopifyApp`](https://github.com/Shopify/shopify_app) gem.
1112

@@ -39,6 +40,8 @@ To instantiate a session, we recommend you either use the `shopify_app` if worki
3940
## Using REST Resources
4041
We provide a templated class library to access REST resources similar to `ActiveResource`. Format of the methods closely resemble our [REST API schema](https://shopify.dev/docs/api/admin-rest).
4142

43+
The version of REST resource that's loaded and used is set from [`ShopifyAPI::Context.setup`](https://github.com/Shopify/shopify-api-ruby/blob/main/README.md#setup-shopify-context)
44+
4245
### Instantiation
4346
Create an instance of the REST resource you'd like to use and optionally provide the following parameters.
4447
#### Constructor parameters

0 commit comments

Comments
 (0)