-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenapi-basic.ts
More file actions
50 lines (41 loc) · 1.46 KB
/
openapi-basic.ts
File metadata and controls
50 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// OpenAPI Key usage — no OAuth required, just set LISTENHUB_API_KEY.
//
// Run: LISTENHUB_API_KEY=lh_sk_... pnpm exec tsx examples/openapi-basic.ts
import {OpenAPIClient, ListenHubError} from '../src/index.js';
const client = new OpenAPIClient();
// 1. List available speakers
const {items: speakers} = await client.listSpeakers({language: 'en'});
console.log(`Available speakers: ${speakers.map((s) => s.name).join(', ')}`);
// 2. Create a flow speech
const speaker = speakers[0]!;
const {episodeId} = await client.createFlowSpeech({
sources: [{type: 'url', content: 'https://en.wikipedia.org/wiki/Mars'}],
speakers: [{speakerId: speaker.speakerId}],
language: 'en',
});
console.log(`Created flow speech: ${episodeId}`);
// 3. Poll until done
let detail = await client.getFlowSpeech(episodeId);
while (detail.processStatus === 'pending') {
await sleep(3000);
detail = await client.getFlowSpeech(episodeId);
console.log(`Status: ${detail.processStatus}`);
}
if (detail.audioUrl) {
console.log(`Audio: ${detail.audioUrl}`);
console.log(`Title: ${detail.title}`);
}
// 4. Check remaining credits
const sub = await client.getSubscription();
console.log(`Credits remaining: ${sub.totalAvailableCredits}`);
// Error handling
try {
await client.getFlowSpeech('nonexistent-id');
} catch (err) {
if (err instanceof ListenHubError) {
console.log(`Expected error: [${err.status}] ${err.message}`);
}
}
function sleep(ms: number) {
return new Promise((r) => setTimeout(r, ms));
}