Skip to content

Authentication

Shopier supports two authentication models:

  1. Personal Access Tokens (PAT): Static tokens generated via the developer portal, ideal for scripts, backend batch jobs, and private single-merchant integrations.
  2. OAuth 2.0: Standard authorization code flow for public multi-tenant applications where merchants grant consent to access their store resources.

1. Personal Access Tokens (PAT)

Pass the token string directly to shopier.NewClient:

go
client, err := shopier.NewClient("pat_token_from_developer_portal")
if err != nil {
	log.Fatal(err)
}

2. OAuth 2.0 Flow

Shopier enforces OAuth 2.0 endpoints through specific ports:

  • Authorization Consent: https://developer.shopier.com/v1/oauth2/authorize
  • Token Exchange & Refresh: https://api.shopier.com:8443/v1/oauth2/token
  • Token Revocation: https://api.shopier.com:8443/v1/oauth2/revoke

The oauth package manages these endpoint requirements automatically.

Initializing OAuth Config

go
import "github.com/AdisGroup/shopier-go/oauth"

cfg := oauth.NewConfig(
	"YOUR_CLIENT_ID",
	"YOUR_CLIENT_SECRET",
	"https://yourapp.com/oauth/callback",
)

Generate the consent page URL with requested permission scopes and a CSRF state token:

go
consentURL := cfg.AuthCodeURL(
	"csrf_state_token",
	oauth.ScopeOrdersRead,
	oauth.ScopeOrdersWrite,
	oauth.ScopeProductsRead,
	oauth.ScopeProductsWrite,
)

// Redirect user to consentURL

Step 2: Exchanging Authorization Code

In your callback HTTP handler, exchange the temporary code for access and refresh tokens:

go
token, err := cfg.Exchange(ctx, code)
if err != nil {
	log.Fatalf("OAuth exchange failed: %v", err)
}

fmt.Printf("Access Token: %s (Expires in %d seconds)\n", token.AccessToken, token.ExpiresIn)
fmt.Printf("Refresh Token: %s\n", token.RefreshToken)

Step 3: Refreshing Expired Tokens

Shopier access tokens expire in 3 days (259,200 seconds). Use the refresh token to request a new access token:

go
if token.Expired() {
	newToken, err := cfg.RefreshToken(ctx, token.RefreshToken)
	if err != nil {
		log.Fatalf("Token refresh failed: %v", err)
	}
	token = newToken
}

Step 4: Revoking Access

To invalidate a token when a merchant disconnects your application:

go
err := cfg.Revoke(ctx, token.AccessToken)
if err != nil {
	log.Fatalf("Token revocation failed: %v", err)
}

Permission Scopes Reference

Scope ConstantScope NamePermitted Operations
oauth.ScopeOrdersReadorders:readList/Get orders, transactions, order webhooks
oauth.ScopeOrdersWriteorders:writeUpdate order fulfillments and tracking numbers
oauth.ScopeProductsReadproducts:readRead products, categories, variations, selections
oauth.ScopeProductsWriteproducts:writeCreate, update, delete products and taxonomy
oauth.ScopeShippingsReadshippings:readRead shipping labels and tracking status
oauth.ScopeShippingsWriteshippings:writeGenerate contracted shipping codes
oauth.ScopeDiscountsReaddiscounts:readRead promo codes and automatic discounts
oauth.ScopeDiscountsWritediscounts:writeCreate and manage discount campaigns
oauth.ScopePayoutsReadpayouts:readRead merchant payout ledgers and balance
oauth.ScopeRefundsReadrefunds:readRead refund records and status
oauth.ScopeRefundsWriterefunds:writeInitiate order refund requests
oauth.ScopeShopReadshop:readRead owner details and storefront settings
oauth.ScopeShopWriteshop:writeUpdate storefront configuration