Skip to content

Commit 0383308

Browse files
Admin API added
1 parent 6c972ed commit 0383308

29 files changed

Lines changed: 903 additions & 7807 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
2+
package-lock.json
23
npm-debug.log*
34
yarn-debug.log*
45
yarn-error.log*

examples/admin-accounts.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { LinkedApiAdmin, LinkedApiError } from '@linkedapi/node';
2+
3+
async function adminAccountsExample(): Promise<void> {
4+
const admin = new LinkedApiAdmin({
5+
linkedApiToken: process.env.LINKED_API_TOKEN!,
6+
});
7+
8+
try {
9+
console.log('🚀 Linked API Admin Accounts example starting...');
10+
11+
await getAllAccounts(admin);
12+
13+
} catch (error) {
14+
if (error instanceof LinkedApiError) {
15+
console.error('🚨 Linked API Error:', error.message);
16+
console.error('📝 Details:', error.details);
17+
} else {
18+
console.error('💥 Unknown error:', error);
19+
}
20+
}
21+
}
22+
23+
async function getAllAccounts(admin: LinkedApiAdmin): Promise<void> {
24+
console.log('\n👥 Getting all connected accounts...');
25+
26+
const { accounts, pendingConnectionSessions } = await admin.accounts.getAll();
27+
28+
console.log('✅ Accounts retrieved');
29+
console.log(` Connected accounts: ${accounts.length}`);
30+
for (const account of accounts) {
31+
console.log(` ${account.name} (${account.status}) – ${account.id}`);
32+
console.log(` Country: ${account.countryCode}`);
33+
console.log(` Connected: ${account.connectedAt}`);
34+
}
35+
36+
if (pendingConnectionSessions.length > 0) {
37+
console.log(`\n Pending sessions: ${pendingConnectionSessions.length}`);
38+
for (const session of pendingConnectionSessions) {
39+
console.log(` Session ${session.sessionId}: ${session.status}`);
40+
}
41+
}
42+
}
43+
44+
async function connectNewAccount(admin: LinkedApiAdmin): Promise<void> {
45+
console.log('\n🔗 Creating connection session...');
46+
47+
const { sessionId, connectionLink } = await admin.accounts.createConnectionSession();
48+
console.log('✅ Connection session created');
49+
console.log(` Session ID: ${sessionId}`);
50+
console.log(` Open this link to connect: ${connectionLink}`);
51+
52+
console.log('\n⏳ Polling connection session status...');
53+
let session;
54+
55+
do {
56+
await new Promise((resolve) => setTimeout(resolve, 3000));
57+
session = await admin.accounts.getConnectionSession({ sessionId });
58+
console.log(` Status: ${session.session.status}`);
59+
} while (
60+
session.session.status === 'pending' ||
61+
session.session.status === 'preparing' ||
62+
session.session.status === 'serving' ||
63+
session.session.status === 'streaming'
64+
);
65+
66+
if (session.session.status === 'success') {
67+
console.log('✅ Account connected successfully!');
68+
69+
const { accounts } = await admin.accounts.getAll();
70+
console.log(` Total accounts: ${accounts.length}`);
71+
} else {
72+
console.log(`❌ Connection failed with status: ${session.session.status}`);
73+
}
74+
}
75+
76+
async function regenerateToken(admin: LinkedApiAdmin): Promise<void> {
77+
const { accounts } = await admin.accounts.getAll();
78+
if (accounts.length === 0) {
79+
console.log('\n⚠️ No accounts to regenerate token for');
80+
return;
81+
}
82+
83+
const accountId = accounts[0].id;
84+
console.log(`\n🔑 Regenerating token for account ${accounts[0].name}...`);
85+
86+
const { token } = await admin.accounts.regenerateIdentificationToken({ accountId });
87+
console.log('✅ Token regenerated');
88+
console.log(` New token: ${token}`);
89+
console.log(' ⚠️ Update identificationToken in all your SDK instances!');
90+
}
91+
92+
if (require.main === module) {
93+
adminAccountsExample();
94+
}

examples/admin-limits.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { LinkedApiAdmin, LinkedApiError } from '@linkedapi/node';
2+
3+
async function adminLimitsExample(): Promise<void> {
4+
const admin = new LinkedApiAdmin({
5+
linkedApiToken: process.env.LINKED_API_TOKEN!,
6+
});
7+
8+
try {
9+
console.log('🚀 Linked API Admin Limits example starting...');
10+
11+
await getDefaultLimits(admin);
12+
await getAccountLimitsAndUsage(admin);
13+
14+
} catch (error) {
15+
if (error instanceof LinkedApiError) {
16+
console.error('🚨 Linked API Error:', error.message);
17+
console.error('📝 Details:', error.details);
18+
} else {
19+
console.error('💥 Unknown error:', error);
20+
}
21+
}
22+
}
23+
24+
async function getDefaultLimits(admin: LinkedApiAdmin): Promise<void> {
25+
console.log('\n📋 Getting default limits...');
26+
27+
const { limits } = await admin.limits.getDefaults();
28+
console.log('✅ Default limits retrieved');
29+
for (const limit of limits) {
30+
const status = limit.isEnabled ? '✅' : '⏸️';
31+
console.log(` ${status} ${limit.category} (${limit.period}): max ${limit.maxValue}`);
32+
}
33+
}
34+
35+
async function getAccountLimitsAndUsage(admin: LinkedApiAdmin): Promise<void> {
36+
const { accounts } = await admin.accounts.getAll();
37+
if (accounts.length === 0) {
38+
console.log('\n⚠️ No accounts to check limits for');
39+
return;
40+
}
41+
42+
const account = accounts[0];
43+
if (!account) {
44+
console.log('\n⚠️ No accounts to check limits for');
45+
return;
46+
}
47+
const accountId = account.id;
48+
49+
console.log(`\n📊 Getting limits for ${account.name}...`);
50+
const { limits } = await admin.limits.get({ accountId });
51+
console.log('✅ Account limits retrieved');
52+
for (const limit of limits) {
53+
console.log(` ${limit.category} (${limit.period}): max ${limit.maxValue}`);
54+
}
55+
56+
console.log(`\n📈 Getting usage for ${account.name}...`);
57+
const { usage } = await admin.limits.getUsage({ accountId });
58+
console.log('✅ Usage retrieved');
59+
for (const entry of usage) {
60+
const remaining = entry.maxValue - entry.currentUsage;
61+
const status = entry.isEnabled ? '✅' : '⏸️';
62+
console.log(` ${status} ${entry.category} (${entry.period}): ${entry.currentUsage}/${entry.maxValue} (${remaining} remaining)`);
63+
}
64+
}
65+
66+
async function setCustomLimits(admin: LinkedApiAdmin): Promise<void> {
67+
const { accounts } = await admin.accounts.getAll();
68+
if (accounts.length === 0) {
69+
console.log('\n⚠️ No accounts to set limits for');
70+
return;
71+
}
72+
const account = accounts[0];
73+
if (!account) {
74+
console.log('\n⚠️ No accounts to check limits for');
75+
return;
76+
}
77+
const accountId = account.id;
78+
79+
console.log(`\n🔧 Setting custom limits for ${account.name}...`);
80+
await admin.limits.set({
81+
accountId,
82+
limits: [
83+
{
84+
category: 'stMessages',
85+
period: 'daily',
86+
maxValue: 25,
87+
isEnabled: true,
88+
},
89+
{
90+
category: 'stConnectionRequests',
91+
period: 'weekly',
92+
maxValue: 30,
93+
},
94+
],
95+
});
96+
console.log('✅ Custom limits set');
97+
98+
console.log('\n🗑️ Deleting specific limits...');
99+
await admin.limits.delete({
100+
accountId,
101+
limits: [
102+
{ category: 'stMessages', period: 'daily' },
103+
],
104+
});
105+
console.log('✅ Limits deleted');
106+
107+
console.log('\n🔄 Resetting all limits to defaults...');
108+
await admin.limits.resetToDefaults({ accountId });
109+
console.log('✅ Limits reset to defaults');
110+
}
111+
112+
if (require.main === module) {
113+
adminLimitsExample();
114+
}

examples/admin-subscription.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { LinkedApiAdmin, LinkedApiError } from '@linkedapi/node';
2+
3+
async function adminSubscriptionExample(): Promise<void> {
4+
const admin = new LinkedApiAdmin({
5+
linkedApiToken: process.env.LINKED_API_TOKEN!,
6+
});
7+
8+
try {
9+
console.log('🚀 Linked API Admin Subscription example starting...');
10+
11+
await getSubscriptionStatus(admin);
12+
await getSeats(admin);
13+
await getPricing(admin);
14+
15+
} catch (error) {
16+
if (error instanceof LinkedApiError) {
17+
console.error('🚨 Linked API Error:', error.message);
18+
console.error('📝 Details:', error.details);
19+
} else {
20+
console.error('💥 Unknown error:', error);
21+
}
22+
}
23+
}
24+
25+
async function getSubscriptionStatus(admin: LinkedApiAdmin): Promise<void> {
26+
console.log('\n📊 Getting subscription status...');
27+
28+
const status = await admin.subscription.getStatus();
29+
console.log('✅ Subscription status retrieved');
30+
console.log(` Status: ${status.status ?? 'no subscription'}`);
31+
console.log(` Cancel at period end: ${status.cancelAtPeriodEnd}`);
32+
}
33+
34+
async function getSeats(admin: LinkedApiAdmin): Promise<void> {
35+
console.log('\n💺 Getting subscription seats...');
36+
37+
const { seats } = await admin.subscription.getSeats();
38+
console.log('✅ Seats retrieved');
39+
console.log(` Total seat groups: ${seats.length}`);
40+
for (const seat of seats) {
41+
console.log(` ${seat.seatType} × ${seat.quantity} (${seat.billingPeriod})`);
42+
}
43+
}
44+
45+
async function getPricing(admin: LinkedApiAdmin): Promise<void> {
46+
console.log('\n💰 Getting pricing information...');
47+
48+
const { products } = await admin.subscription.getPricing();
49+
console.log('✅ Pricing retrieved');
50+
for (const product of products) {
51+
const price = (product.unitPrice / 100).toFixed(2);
52+
const currencySymbol = product.currency === 'eur' ? '€' : '$';
53+
console.log(` ${product.seatType} ${product.billingPeriod}: ${currencySymbol}${price}/seat`);
54+
}
55+
}
56+
57+
async function setSeats(admin: LinkedApiAdmin): Promise<void> {
58+
console.log('\n🔧 Setting subscription seats...');
59+
60+
const result = await admin.subscription.setSeats({
61+
quantity: 5,
62+
seatType: 'plus',
63+
billingPeriod: 'year',
64+
});
65+
66+
if (result.status === 'complete') {
67+
console.log('✅ Seats updated successfully');
68+
} else {
69+
console.log('💳 Checkout required. Complete payment:');
70+
console.log(` ${result.paymentLink}`);
71+
}
72+
}
73+
74+
async function getBillingLink(admin: LinkedApiAdmin): Promise<void> {
75+
console.log('\n🔗 Getting billing portal link...');
76+
77+
const { stripeLink } = await admin.subscription.getBillingLink();
78+
console.log('✅ Billing link retrieved');
79+
console.log(` ${stripeLink}`);
80+
}
81+
82+
if (require.main === module) {
83+
adminSubscriptionExample();
84+
}

examples/advanced-usage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedApi, { LinkedApiError } from 'linkedapi-node';
1+
import LinkedApi, { LinkedApiError } from '@linkedapi/node';
22

33
async function advancedUsageExample(): Promise<void> {
44
const linkedapi = new LinkedApi({

examples/connections.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedApi, { LinkedApiError } from 'linkedapi-node';
1+
import LinkedApi, { LinkedApiError } from '@linkedapi/node';
22

33
async function connectionsExample(): Promise<void> {
44

examples/fetch-company.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedApi, { LinkedApiError } from 'linkedapi-node';
1+
import LinkedApi, { LinkedApiError } from '@linkedapi/node';
22

33
async function fetchCompanyExample(): Promise<void> {
44
const linkedapi = new LinkedApi({

examples/fetch-person.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedApi, { LinkedApiError } from 'linkedapi-node';
1+
import LinkedApi, { LinkedApiError } from '@linkedapi/node';
22

33
async function fetchPersonExample(): Promise<void> {
44

examples/fetch-post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedApi, { LinkedApiError, POST_COMMENTS_SORT } from 'linkedapi-node';
1+
import LinkedApi, { LinkedApiError, POST_COMMENTS_SORT } from '@linkedapi/node';
22

33
async function fetchPostExample(): Promise<void> {
44
const linkedapi = new LinkedApi({

examples/messaging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedApi, { LinkedApiError } from 'linkedapi-node';
1+
import LinkedApi, { LinkedApiError } from '@linkedapi/node';
22

33
async function messagingExample(): Promise<void> {
44
const linkedapi = new LinkedApi({

0 commit comments

Comments
 (0)