Files
cfw-autumn/apps/docs/mintlify/documentation/getting-started/setup/sdk.mdx
2026-02-20 17:33:15 +00:00

210 lines
4.8 KiB
Plaintext

---
title: "Using SDK"
description: "Implement your app's payments and pricing model using the Autumn's server-side SDK"
---
import CreatePlans from '/snippets/create-plans.mdx';
In this example we'll create the pricing for a premium AI chatbot. We're going to have:
- A <Badge color="green">Free</Badge> plan that gives users 5 chat messages per month for free
- A <Badge color="blue">Pro</Badge> plan that gives users 100 chat messages per month for $20 per month.
<Steps>
<Step>
### Create your pricing plans
Create a plan for each tier that your app offers. In our example we'll create a "Free" and "Pro" plan.
<CreatePlans />
</Step>
<Step>
### Installation
[Create an Autumn Secret key](https://app.useautumn.com/sandbox/dev?tab=api_keys), and paste it in your `.env` variables. Then, install the Autumn SDK.
```bash .env
AUTUMN_SECRET_KEY=am_sk_test_42424242...
```
<CodeGroup>
```bash bun
bun add autumn-js
```
```bash npm
npm install autumn-js
```
```bash pnpm
pnpm add autumn-js
```
```bash yarn
yarn add autumn-js
```
```bash pip
pip install autumn-sdk
```
</CodeGroup>
</Step>
<Step>
### Create an Autumn customer
When the customer signs up, create an Autumn customer for them. Autumn will automatically enable the <Badge color="green">Free</Badge> plan, since you marked it with the `auto-enable` flag.
<CodeGroup dropdown>
```typescript
import { Autumn } from "autumn-js";
const autumn = new Autumn({
secretKey: 'am_sk_42424242',
});
const customer = await autumn.customers.getOrCreate({
customerId: "user_or_org_id_from_auth",
name: "John Doe",
email: "john@example.com",
});
```
```python
import asyncio
from autumn_sdk import Autumn
autumn = Autumn('am_sk_42424242')
async def main():
customer = await autumn.customers.get_or_create(
customer_id="user_or_org_id_from_auth",
name="John Doe",
email="john@example.com",
)
asyncio.run(main())
```
```bash cURL
curl --request POST \
--url https://api.useautumn.com/v1/customers \
--header 'Authorization: Bearer am_sk_42424242' \
--header 'Content-Type: application/json' \
--data '{
"customer_id": "user_or_org_id_from_auth",
"name": "John Doe",
"email": "john@example.com"
}'
```
</CodeGroup>
<Check>
Autumn's customer ID is the same as your internal user or org ID generated from your auth provider, so you can use the same ID for everything.
</Check>
In the Autumn dashboard, you will see your user under the [customers](https://app.useautumn.com/customers) page.
</Step>
<Step>
### Stripe Payment Flow
Call `billing.attach` to attach the <Badge color="blue">Pro</Badge> plan to the customer. Redirect the customer to the returned `paymentUrl` to complete payment or confirm the plan change.
<CodeGroup dropdown>
```typescript
import { Autumn } from "autumn-js";
const autumn = new Autumn({
secretKey: 'am_sk_42424242'
});
const response = await autumn.billing.attach({
customerId: "user_or_org_id_from_auth",
planId: "pro",
});
// Redirect customer to complete payment or confirm plan change
redirect(response.paymentUrl);
```
```python
import asyncio
from autumn_sdk import Autumn
autumn = Autumn('am_sk_42424242')
async def main():
response = await autumn.billing.attach(
customer_id='user_or_org_id_from_auth',
plan_id='pro'
)
asyncio.run(main())
```
```bash cURL
curl -X POST 'https://api.useautumn.com/v1/attach' \
-H 'Authorization: Bearer am_sk_42424242' \
-H 'Content-Type: application/json' \
-d '{
"customer_id": "user_or_org_id_from_auth",
"plan_id": "pro"
}'
```
</CodeGroup>
<Note>
Use Stripe's test card `4242 4242 4242 4242` to make a purchase in sandbox. You can enter any Expiry and CVV.
</Note>
This can be used for any plan changes scenario (upgrades, downgrades, one-time topups, renewals, etc).
Upgrades will happen immediately, and downgrades will be scheduled for the next billing cycle.
<Note>
**Default behavior:** For new subscriptions, `paymentUrl` points to **Stripe Checkout**. For plan changes (upgrades/downgrades), it points to **Autumn Checkout** where customers can review prorations before confirming.
**Build your own UI:** If you want to handle the checkout for plan changes yourself:
1. Call [previewAttach](/api-reference/billing/previewAttach) to get line items and pricing details
2. Call `attach` with `redirectMode: "if_required"` — this charges the customer automatically if they have a payment method on file
</Note>
</Step>
</Steps>
**Next: Track and limit usage**
Now that the plan is enabled and you've handled payments, you can now make sure that customers have the correct access and limits based on their plan.
<Card
title="Track and limit usage"
href='/documentation/getting-started/gating'
>
Enforce usage limits and feature permissions using Autumn's `check` and `track` functions
</Card>