3
4export async function dailyDadJoke() {
5 let { setup, punchline } = await fetchJSON("https://official-joke-api.appspot.com/random_joke");
6 return email({
7 text: punchline,
6* Fork this val to your own profile.
7* Make a folder for the temporary vals that get generated, take the ID from the URL, and put it in `tempValsParentFolderId`.
8* If you want to use OpenAI models you need to set the `OPENAI_API_KEY` [env var](https://www.val.town/settings/environment-variables).
9* If you want to use Anthropic models you need to set the `ANTHROPIC_API_KEY` [env var](https://www.val.town/settings/environment-variables).
10* Create a [Val Town API token](https://www.val.town/settings/api), open the browser preview of this val, and use the API token as the password to log in.
11
12<img width=500 src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/7077d1b5-1fa7-4a9b-4b93-f8d01d3e4f00/public"/>
1// This val implements a function to call the Claude API using fetch.
2// It handles the API request and response processing.
3// The Claude API key is expected to be provided in the request headers.
4// CORS is enabled for all origins.
5
11 "Access-Control-Allow-Origin": "*",
12 "Access-Control-Allow-Methods": "POST, OPTIONS",
13 "Access-Control-Allow-Headers": "Content-Type, x-api-key",
14 },
15 });
20 }
21
22 const apiKey = req.headers.get("x-api-key");
23 if (!apiKey) {
24 return new Response("Claude API key not provided in headers", { status: 400 });
25 }
26
30 const systemPrompt = params["system_prompt"];
31 const userPrompt = params["user_prompt"];
32 const claudeResponse = await callClaudeApi(systemPrompt, userPrompt, apiKey);
33 return new Response(claudeResponse, {
34 headers: {
48}
49
50async function callClaudeApi(systemPrompt: string, userPrompt: string, apiKey: string): Promise<string> {
51 const url = "https://api.anthropic.com/v1/messages";
52 const headers = {
53 "Content-Type": "application/json",
54 "x-api-key": apiKey,
55 "anthropic-version": "2023-06-01",
56 "anthropic-beta": "prompt-caching-2024-07-31",
83
84 const responseData = await response.json();
85 console.log("Claude API response:", responseData);
86
87 if (response.ok) {
88 return responseData.content[0].text;
89 } else {
90 throw new Error(`API Error: ${response.status}, ${JSON.stringify(responseData)}`);
91 }
92 } catch (error) {
93 console.error("Error calling Claude API:", error);
94 throw error;
95 }
13 const readmeVals: any = await (await fetch("https://janpaul123-readmevals.web.val.run/")).json();
14
15 const API_URL = "https://api.val.town";
16
17 const sections = [
129 {
130 prompt: "Write a val that accesses environment variables",
131 code: `const res = await fetch("${API_URL}/v1/me", {
132 headers: {
133 Authorization: \`Bearer \${Deno.env.get("valtown")}\`,
141 code: `import process from "node:process";
142
143 const res = await fetch("${API_URL}/v1/me", {
144 headers: {
145 Authorization: \`Bearer \${process.env.valtown}\`,
310 You can attach files to your emails by using the \`attachments\` field.
311 Attachments need to be [Base64](https://en.wikipedia.org/wiki/Base64) encoded,
312 which is that the [btoa](https://developer.mozilla.org/en-US/docs/Web/API/btoa)
313 method is doing in this example:
314
412 \`\`\`
413
414 ### Lower-level API
415
416 We do provide access to the lower-level getter and setters, which are useful if you are storing non-JSON or binary data, need to stream in your response or request data, or do anything else lower-level.
417
418 - \`async get(key: string)\`: Retrieves a blob for a given key.
419 - \`async set(key: string, value: string | BodyInit)\`: Sets the blob value for a given key. See [BodyInit](https://deno.land/api@v1.38.1?s=BodyInit).
420
421 ### Limitations
458 role: "assistant",
459 content:
460 "```ts\nexport default async function(req: Request): Promise<Response> {\n const apiKey = Deno.env.get(\"METEO_KEY\");\n const apiUrl = `https://api.open-meteo.com/v1/forecast?latitude=40.6782&longitude=-73.9442&hourly=temperature_2m¤t_weather=true&apikey=${apiKey}`;\n\n const response = await fetch(apiUrl);\n const weatherData = await response.json();\n\n return new Response(JSON.stringify(weatherData), { headers: { \"Content-Type\": \"application/json\" } });\n}\n```",
461 },
462 {
1* Fork this val to your own profile.
2* Create a [Val Town API token](https://www.val.town/settings/api), open the browser preview of this val, and use the API token as the password to log in.
3
38 model: openai("gpt-4o", {
39 baseURL: "https://std-openaiproxy.web.val.run/v1",
40 apiKey: Deno.env.get("valtown"),
41 } as any),
42 messages: [
6* Fork this val to your own profile.
7* Make a folder for the temporary vals that get generated, take the ID from the URL, and put it in `tempValsParentFolderId`.
8* If you want to use OpenAI models you need to set the `OPENAI_API_KEY` [env var](https://www.val.town/settings/environment-variables).
9* If you want to use Anthropic models you need to set the `ANTHROPIC_API_KEY` [env var](https://www.val.town/settings/environment-variables).
10* Create a [Val Town API token](https://www.val.town/settings/api), open the browser preview of this val, and use the API token as the password to log in.
11
12<img width=500 src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/7077d1b5-1fa7-4a9b-4b93-f8d01d3e4f00/public"/>
94 if (!inputs.$key || inputs.$key != Deno.env.get("BB_SERVICE_KEY")) {
95 return {
96 $error: "Must provide an API key to access the service.",
97 };
98 }
2
3export default async function saveToAirtable(videoName, videoDescription, videoCover, videoFile, clientLogo) {
4 // Set up your Airtable API key and base ID
5 const API_KEY = "patrq3goiFF1u09Dz.b435affff5fb534b9d5c76e2544ddf88612d6a19aebaec5c14f9434b0d10d2e6";
6 const BASE_ID = "appoxM2jYcSNhc5OX";
7 const TABLE_NAME = "videos_from_ssa";
8
9 // Initialize Airtable with your API key
10 Airtable.configure({ apiKey: API_KEY });
11 const base = Airtable.base(BASE_ID);
12
1# Live reload in new tabs
2
3When you're working on an HTML HTTP val in a new tab, it's annoying to have to manually reload the tab on every save. In the Val Town editor, you can hit cmd+enter, but there's nothing like that for a val in a new tab because Val Town doesn't control that new tab (like we control the iframe in the browser preview). However, you control that HTML via the fetch handler you're writing, so you can add a script that polls the Val Town API for the current version number of your val, and reload itself if it detects a new version. This val has a collection of helpers to help you do just that.
4
5## Usage