Get started with Public and Custom Apps
mainPublic and Custom apps require OAuth to obtain an access token from a specific shop.
- Setup Credentials: Initialize the
shopify.Sessionwith your API Key and API Secret. - Generate Auth URL: Create a session and use
create_permission_urlto generate the URL where you will redirect the merchant. - Exchange Code for Token: In your callback handler, use
session.request_token(request_params)to exchange the temporarycodefor a permanentaccess_token. - Activate Session: Use
shopify.ShopifyResource.activate_session(session)to make authorized requests. - Cleanup: It is best practice to call
shopify.ShopifyResource.clear_session()when finished.
import shopify
import binascii
import os
# 1. Setup credentials
shopify.Session.setup(api_key='API_KEY', secret='API_SECRET')
# 2. Generate Auth URL
shop_url = "SHOP_NAME.myshopify.com"
api_version = '2024-07'
state = binascii.b2a_hex(os.urandom(15)).decode("utf-8")
redirect_uri = "http://myapp.com/auth/shopify/callback"
scopes = ['read_products', 'read_orders']
newSession = shopify.Session(shop_url, api_version)
auth_url = newSession.create_permission_url(redirect_uri, scopes, state)
# Redirect user to auth_url
# 3. Exchange code for token (in callback handler)
# request_params contains the 'code' and 'state' from the redirect
session = shopify.Session(shop_url, api_version)
access_token = session.request_token(request_params)
# 4. Make requests
session = shopify.Session(shop_url, api_version, access_token)
shopify.ShopifyResource.activate_session(session)
shop = shopify.Shop.current()
product = shopify.Product.find(179761209)
# 5. Cleanup
shopify.ShopifyResource.clear_session()