@freemius/sdk 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # Freemius TypeScript SDK
2
+
3
+ Official, minimal TypeScript/JavaScript server SDK for integrating Freemius into SaaS & application backends. Provides
4
+ typed API access, hosted checkout helpers, webhook verification, purchase / license / entitlement retrieval, and
5
+ lightweight utilities – without imposing a framework.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @freemius/sdk
11
+ ```
12
+
13
+ Requires Node.js 18+ (or an Edge runtime supporting Web Crypto + standard fetch APIs). See the
14
+ [official documentation](https://freemius.com/help/documentation/saas-sdk/js-sdk/) for full capability reference.
15
+
16
+ ## Core Capabilities
17
+
18
+ Focused, framework‑agnostic primitives for securely interacting with the Freemius platform:
19
+
20
+ - [Typed API Client](https://freemius.com/help/documentation/saas-sdk/js-sdk/api/): Fine-tuned OpenAPI-derived API
21
+ classes to easily interact with the [Freemius REST API](https://freemius.com/help/documentation/api/).
22
+ - [Checkout Utilities](https://freemius.com/help/documentation/saas-sdk/js-sdk/checkout/): Create Checkout URLs or
23
+ Options for the Overlay mode, sandbox mode, redirect result parsing etc.
24
+ - [Webhook Processing](https://freemius.com/help/documentation/saas-sdk/js-sdk/webhooks/): HMAC‑SHA256 signature
25
+ verification + event listener abstraction
26
+ - [Purchases & Entitlements](https://freemius.com/help/documentation/saas-sdk/js-sdk/purchases/): License retrieval,
27
+ entitlement state validation, minimal model helpers
28
+
29
+ See the [full documentation](https://freemius.com/help/documentation/saas-sdk/js-sdk/) for all exported capabilities.
30
+
31
+ ## Initialization
32
+
33
+ You must supply four credentials (obtainable via the Freemius Developer Dashboard):
34
+
35
+ - `productId` – Numeric product identifier
36
+ - `apiKey` – API key (used as bearer credential)
37
+ - `secretKey` – Secret key used for signing (HMAC) and secure operations
38
+ - `publicKey` – RSA public key for license / signature related verification flows
39
+
40
+ Recommended environment variables (example):
41
+
42
+ ```
43
+ FREEMIUS_PRODUCT_ID=12345
44
+ FREEMIUS_API_KEY=...
45
+ FREEMIUS_SECRET_KEY=...
46
+ FREEMIUS_PUBLIC_KEY=-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----
47
+ ```
48
+
49
+ > Hint: You can get the environment variables from the
50
+ > [Freemius Developer Dashboard](http://freemius.com/help/documentation/saas-sdk/js-sdk/installation/#retrieving-keys-from-the-developer-dashboard).
51
+
52
+ ```ts
53
+ import { Freemius } from '@freemius/sdk';
54
+
55
+ export const freemius = new Freemius({
56
+ productId: Number(process.env.FREEMIUS_PRODUCT_ID),
57
+ apiKey: process.env.FREEMIUS_API_KEY!,
58
+ secretKey: process.env.FREEMIUS_SECRET_KEY!,
59
+ publicKey: process.env.FREEMIUS_PUBLIC_KEY!,
60
+ });
61
+ ```
62
+
63
+ > **Edge runtimes**: ensure the secret material is not bundled client‑side; pass via secure runtime environment
64
+ > injection.
65
+
66
+ ### API Client
67
+
68
+ ```ts
69
+ async function getUserByEmail(email: string) {
70
+ const user = await freemius.api.user.retrieveByEmail(email);
71
+ // user has typed shape matching Freemius API spec
72
+ return user;
73
+ }
74
+ ```
75
+
76
+ See also `api.product`, `api.license`, `api.subscription`, `api.payment`, `api.user`, etc.
77
+
78
+ ### Checkout
79
+
80
+ Construct a [hosted checkout URL](http://freemius.com/help/documentation/checkout/hosted-checkout/) or
81
+ [retrieve overlay options](http://freemius.com/help/documentation/checkout/freemius-checkout-buy-button/) (pair with
82
+ [`@freemius/checkout`](https://www.npmjs.com/package/@freemius/checkout?activeTab=readme) on the client):
83
+
84
+ ```ts
85
+ const checkout = await freemius.checkout.create();
86
+ checkout.setCoupon({ code: 'SAVE10' });
87
+ checkout.setTrial('paid');
88
+
89
+ const hostedUrl = checkout.getLink(); // Redirect user or generate email link
90
+ const overlayOptions = checkout.getOptions(); // Serialize & send to frontend for modal embed
91
+ ```
92
+
93
+ ### Webhooks
94
+
95
+ Minimal Node HTTP example (raw body required for signature verification):
96
+
97
+ ```ts
98
+ import { createServer } from 'node:http';
99
+
100
+ const listener = freemius.webhook.createListener();
101
+
102
+ listener.on('license.created', async ({ objects: { license } }) => {
103
+ // Persist or sync license state in your datastore
104
+ console.log('license.created', license.id);
105
+ });
106
+
107
+ const server = createServer(async (req, res) => {
108
+ if (req.url === '/webhook' && req.method === 'POST') {
109
+ await freemius.webhook.processNodeHttp(listener, req, res);
110
+ } else {
111
+ res.statusCode = 404;
112
+ res.end('Not Found');
113
+ }
114
+ });
115
+
116
+ server.listen(3000, () => {
117
+ console.log('Webhook listener active on :3000');
118
+ });
119
+ ```
120
+
121
+ ### Pricing
122
+
123
+ Retrieve pricing metadata (plans, currencies, etc.):
124
+
125
+ ```ts
126
+ async function fetchPricing() {
127
+ return await freemius.pricing.retrieve();
128
+ }
129
+ ```
130
+
131
+ Use this to create your own pricing table on your site.
132
+
133
+ ### Purchase / License Retrieval
134
+
135
+ Resolve purchase data or validate entitlement status:
136
+
137
+ ```ts
138
+ async function retrievePurchase(licenseId: number) {
139
+ const purchase = await freemius.purchase.retrievePurchase(licenseId);
140
+ if (!purchase) throw new Error('Purchase not found');
141
+ return purchase;
142
+ }
143
+ ```
144
+
145
+ ## Security & Operational Notes
146
+
147
+ > Backend Use Only
148
+ >
149
+ > Never initialize the SDK in browser / untrusted contexts. The `secretKey` and `apiKey` are privileged credentials.
150
+
151
+ ## Next.js / React Starter Kit Integration
152
+
153
+ For an end‑to‑end reference (auth, UI components, portal flows, pricing tables), consult the
154
+ [Next.js Integration Guide](https://freemius.com/help/documentation/saas-sdk/framework/nextjs/) and the accompanying
155
+ Starter Kit repository section.