Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/docs/subscriptions/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
sidebar_position: 1
---

# Create Subscription

Create a subscription with no payment integration.

## Code Sample

```typescript
import { Salable } from '@salable/node-sdk';

const salable = new Salable('{{API_KEY}}', 'v2');

await salable.subscriptions.create({
planUuid: '41192f3a-fcfd-46e2-83db-0fd6a288ad5f',
owner: 'orgId_1234',
granteeId: 'userId_1',
});
```

## Parameters

#### Data (_required_)

_Type:_ `CreateSubscriptionInput`

| Option | Type | Description | Required |
|-------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|
| planUuid | string | The ID of the plan the subscription will belong to | ✅ |
| granteeId | string | The ID of the entity who will be assigned to the license. If the plan is per seat and the minimum seat count is above one the granteeId will be assigned to the first license. The rest of the licenses will be unassigned. | ✅ |
| owner | string | The ID of the entity own the subscription. | ✅ |
| cancelAtPeriodEnd | boolean | If true the subscription will cancel after the expiry date has past. If false the subscription will renew. | ❌ |
| expiryDate | string | Overrides the plan default plan interval and length for the first cycle. | ❌ |
| status | string | The status of the create subscription. Allowed values are "ACTIVE" and "TRIALING". | ❌ |

## Return Type

For more information about this request see our API documentation on [Subscription Create](https://docs.salable.app/api/v2#tag/Subscriptions/operation/createSubscripion)
15 changes: 14 additions & 1 deletion src/subscriptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ import {
GetAllInvoicesOptions,
UpdateSubscriptionInput,
GetSubscriptionSeatsOptions,
PaginatedSeats, GetSeatCountResponse, ManageSeatOptions
PaginatedSeats, GetSeatCountResponse, ManageSeatOptions, CreateSubscriptionInput
} from '../types';
import { v2SubscriptionMethods } from './v2';

export type SubscriptionVersions = {
[Version.V2]: {
/**
* Creates a subscription with the details provided
*
* @param {CreateSubscriptionInput} data - The details to create the new subscription with
* @param {CreateSubscriptionInput} data.planUuid - The UUID of the plan associated with the subscription. The planUuid can be found on the Plan view in the Salable dashboard
* @param {CreateSubscriptionInput} data.granteeId - (Optional) The grantee ID for the subscription.
* @param {CreateSubscriptionInput} data.expiryDate - (Optional) Provide a custom expiry date for the subscription; this will override the plan's default interval.
* @param {CreateSubscriptionInput} data.cancelAtPeriodEnd - (Optional) If set to true the subscription will not renew once the endTime date has passed.
* @param {CreateSubscriptionInput} data.quantity - (Optional) The number of seats to create on the subscription. Default is the plan's minimum seat limit. Only applicable to per seat plans.
*
* @returns {Promise<Subscription>} The data for the new subscription created
*/
create: (data: CreateSubscriptionInput) => Promise<Subscription>;
/**
* Retrieves a list of all subscriptions.
*
Expand Down
1 change: 1 addition & 0 deletions src/subscriptions/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import getUrl from '../../utils/get-url';
const baseUrl = `${SALABLE_BASE_URL}/${RESOURCE_NAMES.SUBSCRIPTIONS}`;

export const v2SubscriptionMethods = (request: ApiRequest): SubscriptionVersions['v2'] => ({
create: (data) => request(getUrl(`${baseUrl}`, data), { method: 'POST', body: JSON.stringify(data) }),
getAll: (options) => request(getUrl(baseUrl, options), { method: 'GET' }),
getSeats: (uuid, options) => request(getUrl(`${baseUrl}/${uuid}/seats`, options), { method: 'GET' }),
getSeatCount: (uuid) => request(getUrl(`${baseUrl}/${uuid}/seats/count`), { method: 'GET' }),
Expand Down
12 changes: 12 additions & 0 deletions src/subscriptions/v2/subscriptions-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ describe('Subscriptions V2 Tests', () => {
await deleteTestData();
});

it('create: Should successfully create a subscription without a payment integration', async () => {
const data = await salable.subscriptions.create({
planUuid: testUuids.paidPlanUuid,
owner: 'example',
granteeId: 'test-grantee-id',
status: 'ACTIVE',
expiryDate: '2025-07-06T12:00:00.000Z',
});

expect(data).toEqual(expect.objectContaining(subscriptionSchema));
});

it('getAll: Should successfully fetch subscriptions', async () => {
const data = await salable.subscriptions.getAll();

Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export type SearchParamOptions = Record<string, string | string[] | number | boo

export type SubscriptionStatus = 'ACTIVE' | 'CANCELED' | 'PAUSED' | 'TRIALING' | 'DELETED' | 'PAST_DUE' | 'INCOMPLETE';

export type CreateSubscriptionInput = {
planUuid: string;
granteeId: string;
owner: string;
expiryDate?: string;
cancelAtPeriodEnd?: boolean;
quantity?: number;
status?: 'ACTIVE' | 'TRIALING';
};

export type CreateAdhocLicenseInput = {
planUuid: string;
member: string;
Expand Down