Prerequisites
You will need to have details on:
-
The package(s) the user will subscribe
-
The user who is going to be subscribed to the package
If neither the user nor package exist yet in Kana, then you should create these first:
Create Users
Create new packages
Once these are created, you will need to ensure you know the id of both.
Both the id for a User and a Package are defined by you upon creation in Kana.You can find the identifiers for both in the Dashboard, or by using the package and user queries to pull all respective records and grab the id from the one(s) you need
Code Sample
Basic
import { KanaAdmin } from '@usekana/admin-kana-js'; \
const client = new KanaAdmin({
apiKey: API_KEY // Replace with own Private API Key
});
const { data, error } = await client.subscriptions.create({packageIds: ["basic"], userId: "124"});
console.log(data);
console.log(error);
Example
import { KanaAdmin } from '@usekana/admin-kana-js';
const client = new KanaAdmin({
apiKey: API_KEY // Replace with own Private API Key
});
app.post('/subscribe', function(req, res){
const userId = req.body.user.id; // Assumes user object has been sent in request body to this endpoint
const packageIds = req.body.packageIds // Assumes array of packageIds has been sent in request body to this endpoint
const { data, error } = await client.subscriptions.create({packageIds: packageIds, userId: userId});
if error {
res.status(400).send("We experienced an error when attempting to susbcribe you. Try again or contact support.")
} else {
// Send back JSON response of full susbscription object
res.json(data);
}
});
Next Steps
Congratulations 🎉 You’ve now successfully redirected a user to subscribe
to Kana packages through Stripe.
Upon successful payment, this user will be subscribed to the package(s) and will have access to the features within. You can now implement checks to confirm they should have access to these features (either in your frontend and/or backend) and record usage of these features - which is even more necessary when they are consumable features whereby a customer is provisioned a certain amount.