-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-create-experience.mjs
More file actions
73 lines (62 loc) · 1.8 KB
/
node-create-experience.mjs
File metadata and controls
73 lines (62 loc) · 1.8 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const baseUrl = process.env.RERATO_API_BASE_URL || "https://api.rerato.ai/v1";
const apiKey = process.env.RERATO_API_KEY;
if (!apiKey) {
throw new Error("Missing RERATO_API_KEY. Export it before running this example.");
}
async function rerato(path, options = {}) {
const response = await fetch(`${baseUrl}${path}`, {
...options,
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
...options.headers,
},
});
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`);
}
return response.json();
}
const idem = (name) => `${name}-${Date.now()}`;
const source = await rerato("/sources", {
method: "POST",
headers: { "Idempotency-Key": idem("source") },
body: JSON.stringify({
type: "topic",
title: "Apollo 11",
content: "Create a short, accurate quiz about Apollo 11 for a general audience.",
}),
});
const host = await rerato("/hosts", {
method: "POST",
headers: { "Idempotency-Key": idem("host") },
body: JSON.stringify({
name: "Mission Control",
language: "en",
personality: {
warmth: 0.7,
humor: 0.3,
energy: 0.8,
formality: 0.6,
},
}),
});
const experience = await rerato("/experiences", {
method: "POST",
headers: { "Idempotency-Key": idem("experience") },
body: JSON.stringify({
source_id: source.data?.id || source.id,
host_id: host.data?.id || host.id,
format: "quiz",
title: "Apollo 11 Quick Quiz",
settings: {
question_count: 5,
difficulty: "general",
},
}),
});
const published = await rerato(`/experiences/${experience.data?.id || experience.id}/publish`, {
method: "POST",
headers: { "Idempotency-Key": idem("publish") },
});
console.log(JSON.stringify({ source, host, experience, published }, null, 2));