Introduction
Environment variables provide a mechanism to protect sensitive data, manage configurations, and streamline different environments like development, testing, and production. In Weaverse Hydrogen, you'll frequently interact with a set of predefined variables tailored specifically for the framework.
Essential Environment Variables
Core Variables
SESSION_SECRET
: A secret key used to sign session cookies. For more details, refer to the Remix documentation on using sessions.PUBLIC_STOREFRONT_API_TOKEN
: The public access token for the Storefront API. This is not required if you are using themock.shop
demo setup.PUBLIC_STORE_DOMAIN
: The domain of the store used to communicate with the Storefront API.PUBLIC_CHECKOUT_DOMAIN
: The domain of the checkout page (or the original Online Store domain).PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID
: The public access token for the Customer Account API.PUBLIC_CUSTOMER_ACCOUNT_API_URL
: The API URL for the Customer Account API, format:https://shopify.com/{SHOP_ID}
.SHOP_ID
: Your Shopify store ID.
Weaverse Specific Variables
WEAVERSE_PROJECT_ID
: A unique ID representing your specific Weaverse project. You can find this ID inside the Weaverse Studio under Project Settings.WEAVERSE_API_KEY
(optional): Weaverse API Key retrieved from your Weaverse Account settings.WEAVERSE_HOST
(optional): The host URL for Weaverse services. Default:https://studio.weaverse.io
.
Optional Variables
PUBLIC_STOREFRONT_ID
: The ID of your storefront.PRIVATE_STOREFRONT_API_TOKEN
: The private access token for the Storefront API.PUBLIC_STOREFRONT_API_VERSION
: The Storefront API version. Defaults to the version used by Hydrogen.
Third-Party Integration Variables
PUBLIC_GOOGLE_GTM_ID
: Google Tag Manager ID for analytics.JUDGEME_PRIVATE_API_TOKEN
: API token for Judge.me reviews integration.KLAVIYO_PRIVATE_API_TOKEN
: API token for Klaviyo integration.PUBLIC_SHOPIFY_INBOX_SHOP_ID
: Shopify Inbox shop ID for chat functionality.
Custom Metafields & Metaobjects
METAOBJECT_COLORS_TYPE
: Type identifier for color metaobjects.CUSTOM_COLLECTION_BANNER_METAFIELD
: Metafield identifier for collection banners.
Setting Up Environment Variables
Using Demo Setup (with mock.shop
)
SESSION_SECRET="my-secret-key"PUBLIC_STORE_DOMAIN="mock.shop"WEAVERSE_PROJECT_ID="weaverse-project-id"
Production Setup
# Core VariablesSESSION_SECRET="your-session-secret"PUBLIC_STORE_DOMAIN="your-store.myshopify.com"PUBLIC_CHECKOUT_DOMAIN="your-store.myshopify.com"PUBLIC_STOREFRONT_API_TOKEN="your-storefront-api-token"PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID="your-customer-account-api-client-id"PUBLIC_CUSTOMER_ACCOUNT_API_URL="https://shopify.com/your-shop-id"SHOP_ID="your-shop-id"
# Weaverse VariablesWEAVERSE_PROJECT_ID="your-weaverse-project-id"WEAVERSE_API_KEY="your-weaverse-api-key" # OptionalWEAVERSE_HOST="https://studio.weaverse.io" # Optional
# Optional VariablesPUBLIC_STOREFRONT_ID="your-storefront-id" # OptionalPRIVATE_STOREFRONT_API_TOKEN="your-private-storefront-api-token" # Optional
# Third-Party IntegrationsPUBLIC_GOOGLE_GTM_ID="your-gtm-id" # OptionalJUDGEME_PRIVATE_API_TOKEN="your-judgeme-token" # OptionalKLAVIYO_PRIVATE_API_TOKEN="your-klaviyo-token" # OptionalPUBLIC_SHOPIFY_INBOX_SHOP_ID="your-inbox-shop-id" # Optional
# Custom MetafieldsMETAOBJECT_COLORS_TYPE="shopify--color-pattern" # OptionalCUSTOM_COLLECTION_BANNER_METAFIELD="custom.collection_banner" # Optional
TypeScript Support
For TypeScript support, add your environment variables to env.d.ts
:
interface Env extends HydrogenEnv { // Core Variables PUBLIC_STORE_DOMAIN: string; PUBLIC_STOREFRONT_API_TOKEN: string; PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID: string; SHOP_ID: string; // Weaverse Variables WEAVERSE_PROJECT_ID: string; WEAVERSE_API_KEY?: string; WEAVERSE_HOST?: string; // Optional Variables PUBLIC_STOREFRONT_ID?: string; PRIVATE_STOREFRONT_API_TOKEN?: string; // Third-Party Integrations PUBLIC_GOOGLE_GTM_ID?: string; JUDGEME_PRIVATE_API_TOKEN?: string; KLAVIYO_PRIVATE_API_TOKEN?: string; PUBLIC_SHOPIFY_INBOX_SHOP_ID?: string; // Custom Metafields METAOBJECT_COLORS_TYPE?: string; CUSTOM_COLLECTION_BANNER_METAFIELD?: string;}
Best Practices
-
Security:
- Never commit
.env
files to version control - Use
.env.example
for required variables - Keep sensitive tokens private
- Rotate tokens regularly
- Never commit
-
Organization:
- Group related variables together
- Use clear, descriptive names
- Document all variables in your project
- Keep development and production variables separate
-
Development:
- Use different values for development and production
- Validate environment variables at startup
- Provide fallback values where appropriate
- Use TypeScript for type safety
Obtaining Required Tokens
Storefront API Token
- Install the Headless or Hydrogen app
- Follow these instructions to obtain the Storefront API Token
- When setting permissions, enable all Storefront API access scopes to avoid unexpected errors during development
💡 Tip: Always enable all Storefront API permissions when generating your token. This prevents access-related errors that can be difficult to troubleshoot during development.
Weaverse Project ID
- Log in to Weaverse Studio
- Navigate to Project Settings
- Copy your Project ID
Using Environment Variables
Access environment variables in your code:
// In route loadersexport async function loader({ context }: LoaderArgs) { const { env } = context; return json({ storeDomain: env.PUBLIC_STORE_DOMAIN });}
// In Weaverse componentsexport let loader = async ({ weaverse }: ComponentLoaderArgs<{}, Env>) => { const { env } = weaverse; return { projectId: env.WEAVERSE_PROJECT_ID };}
Next Steps
Learn about creating and managing Weaverse Components – the foundational building blocks of your theme.