subtopia-js-sdk

npm version


Subtopia JS SDK is a JavaScript library for interacting with the Subtopia Platform. It provides a simple interface for creating and managing Products (Contracts that are responsible for subscription management).

For detailed documentation, refer to sdk.subtopia.io.

  • subtopia-js-examples - A separate repository with examples of using the Subtopia JS SDK in React, Svelte, Vue and NextJS.
# with npm
npm install subtopia-js-sdk

# or with yarn
yarn add subtopia-js-sdk
import { SubtopiaClient, SubtopiaRegistryClient } from "subtopia-js-sdk";

Example snippets of using the Subtopia JS SDK.

import {
SubtopiaClient,
SubtopiaRegistryClient,
ChainType,
SUBTOPIA_REGISTRY_ID
} from "subtopia-js-sdk";
// ... your code

const subtopiaClient = await SubtopiaClient.init({
algodClient: PUT_ALGOD_INSTANCE_HERE,
chainType: PUT_CHAIN_TYPE_ENUM_HERE // 'testnet'|'mainnet'|'localnet'
productID: PUT_PRODUCT_ID_HERE,
registryID: SUBTOPIA_REGISTRY_ID(ChainType.{YOUR_CHAIN_TYPE_HERE}),
creator: { addr: PUT_WALLET_ADDRESS, signer: PUT_WALLET_SIGNER },
});

const response = await subtopiaClient.createSubscription(
{ subscriber: { addr: PUT_WALLET_ADDRESS, signer: PUT_WALLET_SIGNER } },
);

console.log(response.txID); // response is of type string

// ... rest of your code
// ... your code
const subscriptionRecord = await subtopiaClient.getSubscription({
subscriberAddress: PUT_SUBSCRIBER_ADDRESS,
algodClient: PUT_ALGOD_INSTANCE_HERE,
});

// SubscriptionRecord (throws Error if not subscribed)
console.log(subscriptionRecord);
// ... rest of your code
// ... your code
const deleteResult = await subtopiaClient.deleteSubscription({
subscriber: {
addr: PUT_SUBSCRIBER_ADDRESS,
signer: PUT_SUBSCRIBER_SIGNER,
},
subscriptionID: PUT_SUBSCRIPTION_ID,
});

// Transaction ID of the unsubscribe transaction
console.log(deleteResult.txID);
// ... your code
// ... your code
const transferResult = await subtopiaClient.transferSubscription({
oldSubscriber: {
addr: PUT_OLD_SUBSCRIBER_ADDRESS,
signer: PUT_OLD_SUBSCRIBER_SIGNER,
},
newSubscriberAddress: PUT_NEW_SUBSCRIBER_ADDRESS,
subscriptionID: PUT_SUBSCRIPTION_ID,
});

// Transaction ID of the transfer transaction
console.log(transferResult.txID);
// ... your code
// ... your code

const subtopiaRegistryClient = await SubtopiaRegistryClient.init({
algodClient: PUT_ALGOD_INSTANCE_HERE,
creator: { addr: PUT_WALLET_ADDRESS, signer: PUT_WALLET_SIGNER },
chainType: PUT_CHAIN_TYPE_HERE,
});

const discount = await subtopiaRegistryClient.createDiscount({
discountType: PUT_DISCOUNT_TYPE_HERE, // number - the type of discount to apply. FIXED or PERCENTAGE
discountValue: PUT_DISCOUNT_VALUE_HERE, // number - the discount to be deducted from the subscription price
expiresIn: PUT_EXPIRATION_TIME_HERE, // (Optional) Set 0 for discount to never expire. Else set number of seconds to append to unix timestamp at time of creation.
});

console.log(discount.txID); // response is of type string

// ... rest of your code
// ... your code

const discount = await subtopiaClient.getDiscount();

// DiscountRecord (throws Error if no active discount)
console.log(discount);
// ... rest of your code
// ... your code

const deleteResult = await subtopiaRegistryClient.deleteDiscount();

// Transaction ID of the delete discount transaction
console.log(deleteResult.txID);
// ... your code

Special thanks to everyone who starred the repository ❤️

Stargazers repo roster for @subtopia-algo/subtopia-js-sdk