This guide will walk you through setting up Prices as Code and creating your first pricing configuration.
Create a pricing.ts
file:
import { Config } from 'prices-as-code';
const config: Config = {
products: [
{
provider: 'stripe',
name: 'Basic Plan',
description: 'For individuals and small teams',
features: ['5 projects', '10GB storage', 'Email support'],
highlight: false,
metadata: {
displayOrder: 1
}
},
{
provider: 'stripe',
name: 'Pro Plan',
description: 'For growing businesses',
features: ['Unlimited projects', '100GB storage', 'Priority support'],
highlight: true,
metadata: {
displayOrder: 2
}
}
],
prices: [
{
provider: 'stripe',
name: 'Basic Monthly',
nickname: 'Basic Monthly',
unitAmount: 999, // $9.99
currency: 'usd',
type: 'recurring',
recurring: {
interval: 'month',
intervalCount: 1
},
productKey: 'basic_plan',
metadata: {
displayName: 'Basic Monthly'
}
},
{
provider: 'stripe',
name: 'Pro Monthly',
nickname: 'Pro Monthly',
unitAmount: 1999, // $19.99
currency: 'usd',
type: 'recurring',
recurring: {
interval: 'month',
intervalCount: 1
},
productKey: 'pro_plan',
metadata: {
displayName: 'Pro Monthly'
}
}
]
};
export default config;
Create a prices.yml
file:
products:
- provider: stripe
name: Basic Plan
description: For individuals and small teams
features:
- 5 projects
- 10GB storage
- Email support
highlight: false
metadata:
displayOrder: 1
- provider: stripe
name: Pro Plan
description: For growing businesses
features:
- Unlimited projects
- 100GB storage
- Priority support
highlight: true
metadata:
displayOrder: 2
prices:
- provider: stripe
name: Basic Monthly
nickname: Basic Monthly
unitAmount: 999
currency: usd
type: recurring
recurring:
interval: month
intervalCount: 1
productKey: basic_plan
metadata:
displayName: Basic Monthly
- provider: stripe
name: Pro Monthly
nickname: Pro Monthly
unitAmount: 1999
currency: usd
type: recurring
recurring:
interval: month
intervalCount: 1
productKey: pro_plan
metadata:
displayName: Pro Monthly
Running the Synchronization
Using the CLI
Sync your configuration using the command-line interface:
npx prices-as-code pricing.ts
Or for YAML:
npx prices-as-code prices.yml
Push Model vs. Write-Back Model
By default, Prices as Code uses a Push Model, where it pushes your configuration to the provider
but doesn't modify your local configuration files. This is useful when:
- You want to use the same configuration file to push to different environments (dev, staging, production)
- You're working with providers that don't allow setting IDs programmatically
- You want to maintain a clean separation between your configuration and provider-specific IDs
If you prefer the original behavior where provider IDs are written back to your configuration file, use the --write-back
flag:
npx prices-as-code prices.yml --write-back
Using the JavaScript API
You can also use the JavaScript API in your own scripts:
import { pac } from 'prices-as-code';
async function syncPricing() {
try {
const result = await pac({
configPath: './pricing.ts',
providers: [
{
provider: 'stripe',
options: {
secretKey: process.env.STRIPE_SECRET_KEY,
}
}
],
// Set to true to write updated IDs back to config file (legacy behavior)
// Default is false - PUSH mode (push to provider without modifying source)
writeBack: false
});
console.log('Sync result:', result);
} catch (error) {
console.error('Sync failed:', error);
}
}
syncPricing();
Sample Configuration
Real-World Example
Here's a comprehensive example based on a tiered subscription model:
products:
- name: Free
description: Start your learning journey
metadata:
icon: Book
color: blue
key: free
features:
- Access to free content
- Limited features
- Core lessons
- Ad-supported experience
highlight: false
provider: stripe
key: free
- name: Pro
description: Help us shine bright with new features
metadata:
icon: Star
color: purple
key: pro
features:
- Support ongoing development
- Supporter recognition
- Early access to new features
- Ad-free experience
- Unlimited access
highlight: true
provider: stripe
key: pro
- name: Ultra
description: The ultimate experience
metadata:
icon: Trophy
color: amber
key: ultra
features:
- All Pro perks
- Ultra member recognition
- Input on future features
- Priority support
- Personalized plans
highlight: false
provider: stripe
key: ultra
prices:
- name: Free
unitAmount: 0
currency: usd
type: recurring
recurring:
interval: year
intervalCount: 1
active: true
metadata:
plan_code: free
display_price: $0
nickname: Free plan
provider: stripe
productKey: free
- name: Pro Monthly
unitAmount: 1000
currency: usd
type: recurring
recurring:
interval: month
intervalCount: 1
active: true
metadata:
plan_code: pro_monthly
display_price: $10
nickname: Pro Monthly Plan
provider: stripe
productKey: pro
- name: Pro Yearly
unitAmount: 9600
currency: usd
type: recurring
recurring:
interval: year
intervalCount: 1
active: true
metadata:
plan_code: pro_yearly
display_price: $96
nickname: Pro Yearly Plan
provider: stripe
productKey: pro
Next Steps