@dream-api/sdk 0.1.0

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,231 @@
1
+ # @dream-api/sdk
2
+
3
+ Official SDK for Dream API - Auth, billing, and usage tracking in one API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @dream-api/sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { DreamAPI } from '@dream-api/sdk';
15
+
16
+ const api = new DreamAPI({
17
+ secretKey: process.env.DREAM_API_SECRET_KEY,
18
+ publishableKey: process.env.DREAM_API_PUBLISHABLE_KEY,
19
+ });
20
+ ```
21
+
22
+ ## Backend Operations (SK Only)
23
+
24
+ These operations only require your secret key:
25
+
26
+ ### Create Customer
27
+
28
+ ```typescript
29
+ const { customer } = await api.customers.create({
30
+ email: 'user@example.com',
31
+ firstName: 'John',
32
+ plan: 'free',
33
+ });
34
+ ```
35
+
36
+ ### Delete Customer
37
+
38
+ ```typescript
39
+ await api.customers.delete(customerId);
40
+ ```
41
+
42
+ ### Get Dashboard Metrics
43
+
44
+ ```typescript
45
+ const dashboard = await api.dashboard.get();
46
+ console.log(`MRR: $${dashboard.mrr}`);
47
+ console.log(`Active subs: ${dashboard.activeSubscriptions}`);
48
+ ```
49
+
50
+ ### List Products/Tiers
51
+
52
+ ```typescript
53
+ const { tiers } = await api.products.listTiers();
54
+ const { products } = await api.products.list();
55
+ ```
56
+
57
+ ## Frontend Operations (Requires User Token)
58
+
59
+ After user signs in via Clerk, set the token:
60
+
61
+ ```typescript
62
+ // Get token from Clerk
63
+ const token = await clerk.session.getToken();
64
+ api.setUserToken(token);
65
+ ```
66
+
67
+ ### Track Usage
68
+
69
+ ```typescript
70
+ const { usage } = await api.usage.track();
71
+ console.log(`Used ${usage.used} of ${usage.limit}`);
72
+ ```
73
+
74
+ ### Check Usage
75
+
76
+ ```typescript
77
+ const usage = await api.usage.check();
78
+ if (usage.remaining <= 0) {
79
+ // Show upgrade prompt
80
+ }
81
+ ```
82
+
83
+ ### Create Checkout (Subscription Upgrade)
84
+
85
+ ```typescript
86
+ const { url } = await api.billing.createCheckout({
87
+ tier: 'pro',
88
+ successUrl: 'https://yourapp.com/success',
89
+ cancelUrl: 'https://yourapp.com/pricing',
90
+ });
91
+ window.location.href = url;
92
+ ```
93
+
94
+ ### Open Customer Portal
95
+
96
+ ```typescript
97
+ const { url } = await api.billing.openPortal({
98
+ returnUrl: 'https://yourapp.com/dashboard',
99
+ });
100
+ window.location.href = url;
101
+ ```
102
+
103
+ ## Auth URL Helpers
104
+
105
+ ### Sign Up URL
106
+
107
+ ```typescript
108
+ const signupUrl = api.auth.getSignUpUrl({
109
+ redirect: 'https://yourapp.com/dashboard',
110
+ });
111
+
112
+ // Use in your app
113
+ <a href={signupUrl}>Sign Up</a>
114
+ ```
115
+
116
+ ### Sign In URL
117
+
118
+ After initial signup, users sign in via your Clerk instance directly.
119
+
120
+ ## Store/E-commerce
121
+
122
+ ### Cart Checkout (Guest)
123
+
124
+ ```typescript
125
+ const { url } = await api.products.cartCheckout({
126
+ items: [
127
+ { priceId: 'price_xxx', quantity: 2 },
128
+ { priceId: 'price_yyy', quantity: 1 },
129
+ ],
130
+ customerEmail: 'buyer@example.com',
131
+ customerName: 'Jane Doe',
132
+ successUrl: 'https://yourapp.com/success',
133
+ cancelUrl: 'https://yourapp.com/cart',
134
+ });
135
+ ```
136
+
137
+ ## Error Handling
138
+
139
+ ```typescript
140
+ import { DreamAPIException } from '@dream-api/sdk';
141
+
142
+ try {
143
+ await api.usage.track();
144
+ } catch (error) {
145
+ if (error instanceof DreamAPIException) {
146
+ if (error.status === 403) {
147
+ // Usage limit exceeded
148
+ console.log('Upgrade required');
149
+ } else if (error.status === 401) {
150
+ // Token expired or invalid
151
+ console.log('Please sign in again');
152
+ }
153
+ }
154
+ }
155
+ ```
156
+
157
+ ## TypeScript
158
+
159
+ Full TypeScript support with exported types:
160
+
161
+ ```typescript
162
+ import type {
163
+ Customer,
164
+ Usage,
165
+ Tier,
166
+ DashboardMetrics,
167
+ DreamAPIConfig,
168
+ } from '@dream-api/sdk';
169
+ ```
170
+
171
+ ## Environment Variables
172
+
173
+ ```env
174
+ DREAM_API_SECRET_KEY=sk_test_xxx
175
+ DREAM_API_PUBLISHABLE_KEY=pk_test_xxx
176
+ ```
177
+
178
+ ## Framework Examples
179
+
180
+ ### React
181
+
182
+ ```tsx
183
+ import { DreamAPI } from '@dream-api/sdk';
184
+ import { useAuth } from '@clerk/clerk-react';
185
+
186
+ const api = new DreamAPI({
187
+ secretKey: import.meta.env.VITE_DREAM_API_SECRET_KEY,
188
+ publishableKey: import.meta.env.VITE_DREAM_API_PUBLISHABLE_KEY,
189
+ });
190
+
191
+ function Dashboard() {
192
+ const { getToken } = useAuth();
193
+ const [usage, setUsage] = useState(null);
194
+
195
+ useEffect(() => {
196
+ async function loadUsage() {
197
+ const token = await getToken();
198
+ api.setUserToken(token);
199
+ const data = await api.usage.check();
200
+ setUsage(data);
201
+ }
202
+ loadUsage();
203
+ }, []);
204
+
205
+ return <div>Used: {usage?.used} / {usage?.limit}</div>;
206
+ }
207
+ ```
208
+
209
+ ### Next.js (API Route)
210
+
211
+ ```typescript
212
+ // app/api/track/route.ts
213
+ import { DreamAPI } from '@dream-api/sdk';
214
+
215
+ const api = new DreamAPI({
216
+ secretKey: process.env.DREAM_API_SECRET_KEY!,
217
+ });
218
+
219
+ export async function POST(request: Request) {
220
+ const token = request.headers.get('Authorization')?.replace('Bearer ', '');
221
+ api.setUserToken(token!);
222
+
223
+ const result = await api.usage.track();
224
+ return Response.json(result);
225
+ }
226
+ ```
227
+
228
+ ## Support
229
+
230
+ - Documentation: https://docs.dream-api.com
231
+ - Issues: https://github.com/dream-api/sdk/issues