Clerk Billing for B2B SaaS
Clerk Billing for B2B SaaS allows you to create Plans and manage Subscriptions for companies or organizations in your application.
Enable Billing
To enable Billing for your application, navigate to the Billing Settings page in the Clerk Dashboard. This page will guide you through enabling Billing for your application.
Create a Plan
Subscription Plans are what your customers subscribe to. There is no limit to the number of Plans you can create.
To create a Plan, navigate to the Plans page in the Clerk Dashboard. Here, you can create, edit, and delete Plans. To setup B2B billing, select the Plans for Organizations tab and click Add Plan. When creating a Plan, you can also create Features for the Plan; see the next section for more information.
Add Features to a Plan
Features make it easy to give entitlements to your Plans. You can add any number of Features to a Plan.
You can add a Feature to a Plan when you are creating a Plan. To add it after a Plan is created:
- Navigate to the Plans page in the Clerk Dashboard.
- Select the Plan you'd like to add a Feature to.
- In the Features section, select Add Feature.
Create a pricing page
You can create a pricing page by using the <PricingTable />
component. This component displays a table of Plans and Features that customers can subscribe to. It's recommended to create a dedicated page, as shown in the following example.
import { PricingTable } from '@clerk/nextjs'
export default function Page() {
return (
<div style={{ maxWidth: '800px', margin: '0 auto', padding: '0 1rem' }}>
<PricingTable />
</div>
)
}
Control access with Features, Plans, and Permissions
You can use Clerk's Features and Plans to gate access to the content. There are a few ways to do this, but the recommended and simplest way is either using the has()
method or the <Protect>
component.
The has()
method is available for any JavaScript framework, while <Protect>
is only available for React-based frameworks.
Example: Using has()
Use the has()
method to test if the organization has access to a Plan:
const { has } = await auth()
const hasPremiumAccess = has({ plan: 'gold' })
Or a Feature:
const { has } = await auth()
const hasPremiumAccess = has({ feature: 'widgets' })
The has()
method checks if the organization has been granted a specific type of access control (Role, Permission, Feature, or Plan) and returns a boolean value. It is available on the auth
object on the server. Depending on the framework you are using, you will access the auth
object differently.
The following example accesses the auth
object and the has()
method using the Next.js-specific auth()
helper.
The following example demonstrates how to use has()
to check if an organization has a Plan.
import { auth } from '@clerk/nextjs/server'
export default async function Page() {
// Use `auth()` helper to access the `has()` method
const { has } = await auth()
// Use `has()` method to check if an organization has a Plan
const hasBronzePlan = has({ plan: 'bronze' })
if (!hasBronzePlan) return <h1>Only subscribers to the Bronze plan can access this content.</h1>
return <h1>For Bronze subscribers only</h1>
}
The following example demonstrates how to use has()
to check if an organization has a Feature.
import { auth } from '@clerk/nextjs/server'
export default async function Page() {
// Use `auth()` helper to access the `has()` method
const { has } = await auth()
// Use `has()` method to check if an organization has a Feature
const hasPremiumAccess = has({ feature: 'premium_access' })
if (!hasPremiumAccess)
return <h1>Only subscribers with the Premium Access feature can access this content.</h1>
return <h1>Our Exclusive Content</h1>
}
The following example demonstrates how to use has()
to check if an organization has a Permission.
import { auth } from '@clerk/nextjs/server'
export default async function Page() {
// Use `auth()` helper to access the `has()` method
const { has } = await auth()
// Use `has()` method to check if organization has a Permission
const hasPremiumAccessManage = has({ permission: 'premium_access:manage' })
if (!hasPremiumAccessManage)
return (
<h1>Only subscribers with the Premium Access Manage permission can access this content.</h1>
)
return <h1>Our Exclusive Content</h1>
}
Example: Using <Protect>
The <Protect>
component protects content or even entire routes by checking if the organization has been granted a specific type of access control (Role, Permission, Feature, or Plan). You can pass a fallback
prop to <Protect>
that will be rendered if the organization does not have the access control.
The following example demonstrates how to use <Protect>
to protect a page by checking if the organization has a Plan.
export default function ProtectPage() {
return (
<Protect
plan="bronze"
fallback={<p>Only subscribers to the Bronze plan can access this content.</p>}
>
{children}
</Protect>
)
}
The following example demonstrates how to use <Protect>
to protect a page by checking if the organization has a Feature.
export default function ProtectPage() {
return (
<Protect
feature="premium_access"
fallback={<p>Only subscribers with the Premium Access feature can access this content.</p>}
>
{children}
</Protect>
)
}
The following example demonstrates how to use <Protect>
to protect a page by checking if the organization has a Permission.
export default function ProtectPage() {
return (
<Protect
permission="premium_access:manage"
fallback={
<p>Only subscribers with the Premium Access Manage permission can access this content.</p>
}
>
{children}
</Protect>
)
}
Feedback
Last updated on