Skip to content

Commit 91bde9d

Browse files
committed
docs: client credentials grant in shopify-api-js
1 parent 10e8848 commit 91bde9d

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

docs/usage/oauth.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ For more information on authenticating a Shopify app please see the [Types of Au
1212
- [Performing OAuth](#performing-oauth-1)
1313
- [Token Exchange](#token-exchange)
1414
- [Authorization Code Grant Flow](#authorization-code-grant-flow)
15+
- [Client Credentials Grant](#client-credentials-grant)
1516
- [Using OAuth Session to make authenticated API calls](#using-oauth-session-to-make-authenticated-api-calls)
1617

1718
## Session Persistence
@@ -35,6 +36,10 @@ with [token exchange](#token-exchange) instead of the authorization code grant f
3536
- OAuth flow that requires the app to redirect the user to Shopify for installation/authorization of the app to access the shop's data.
3637
- Suitable for non-embedded apps
3738
- Installations, and access scope changes are managed by the app
39+
3. [Client Credentials Grant](#client-credentials-grant)
40+
- Suitable for backend apps without UI
41+
- Doesn't require user interaction in the browser
42+
- Access scopes can be configured either in the Developer Dashboard when creating an app version or in your app's [TOML configuration file](https://shopify.dev/docs/apps/build/cli-for-apps/app-configuration#access_scopes)
3843

3944
## Note about Rails
4045
If using in the Rails framework, we highly recommend you use the [shopify_app](https://github.com/Shopify/shopify_app) gem to perform OAuth, you won't have to follow the instructions below to start your own OAuth flow.
@@ -49,6 +54,8 @@ If you aren't using Rails, you can look at how the `ShopifyApp` gem handles OAut
4954
- Triggering and redirecting user to **begin** OAuth flow
5055
- [Callback Controller](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/callback_controller.rb)
5156
- Creating / storing sessions to **complete** the OAuth flow
57+
- [Client Credentials](https://github.com/Shopify/shopify_app/blob/main/lib/shopify_app/auth/client_credentials.rb)
58+
- Completes client credentials flow to get offline access tokens with expiration time.
5259

5360
## Performing OAuth
5461
### Token Exchange
@@ -265,9 +272,41 @@ def callback
265272
end
266273
end
267274
```
268-
269275
⚠️ You can see a concrete example in the `ShopifyApp` gem's [CallbackController](https://github.com/Shopify/shopify_app/blob/main/app/controllers/shopify_app/callback_controller.rb).
270276

277+
### Client Credentials Grant
278+
279+
> [!NOTE]
280+
> You should consider using the client credentials grant only when building apps for your own organization.
281+
282+
> [!WARNING]
283+
> [token exchange](#token-exchange) (for embedded apps) or the [authorization code grant flow](#authorization-code-grant-flow) should be used instead of the client credentials grant, if your app is a browser based web app.
284+
285+
#### Perform Client Credentials Grant
286+
Use [`ShopifyAPI::Auth::ClientCredentials`](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/auth/client_credentials.rb) to
287+
exchange the [app's client ID and client secret](https://shopify.dev/docs/apps/build/authentication-authorization/client-secrets) for an access token.
288+
#### Input
289+
| Parameter | Type | Required? | Default Value | Notes |
290+
| -------------- | ---------------------- | :-------: | :-----------: | ----------------------------------------------------------------------------------------------------------- |
291+
| `shop` | `String` | Yes | - | A Shopify domain name in the form `{exampleshop}.myshopify.com`. |
292+
293+
#### Output
294+
This method returns the new `ShopifyAPI::Auth::Session` object from the client credentials grant, your app should store this `Session` object to be used later [when making authenticated API calls](#using-oauth-session-to-make-authenticated-api-calls).
295+
296+
#### Example
297+
```ruby
298+
299+
# `shop` is the shop domain name - "this-is-my-example-shop.myshopify.com"
300+
301+
def authenticate(shop)
302+
session = ShopifyAPI::Auth::ClientCredentials.client_credentials(
303+
shop: shop,
304+
)
305+
SessionRepository.store_session(session)
306+
end
307+
308+
```
309+
271310
## Using OAuth Session to make authenticated API calls
272311
Once your OAuth flow is complete, and you have persisted your `Session` object, you may use that `Session` object to make authenticated API calls.
273312

0 commit comments

Comments
 (0)