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
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
const randomId = () => Math.random().toString(36).substring(2, 8);
const createResult = await fetchJSON(`https://api.val.town/v1/vals`, {
method: "POST",
bearer: Deno.env.get("valtown"),
body: JSON.stringify({
code: `console.log(42);`,
name: `api_test_${randomId()}`,
}),
});
console.log("Created with name:", createResult.name);
const newName = `api_test_updated_${randomId()}`;
const updateResult = await fetch(`https://api.val.town/v1/vals/${createResult.id}`, {
method: "PUT",
headers: {
Authorization: `Bearer ${Deno.env.get("valtown")}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: newName,
privacy: "unlisted",
}),
});
console.log("Updated val:", `https://val.town/v/${createResult.author.username.replace(/^@/, "")}/${newName}`);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
December 7, 2023