Air quality API examples & templates
Use these vals as a playground to view and fork Air quality API examples and templates on Val Town. Run any example below or find templates that can be used as a pre-built solution.
maxm
valSession
HTTP
Val Session import { ValSession } from "https://esm.town/v/maxm/valSession";
// Generate a token from your valtown api key.
const token = await ValSession.newSession(Deno.env.get("valtown"));
// Other services can use it to authenticate
const user = await ValSession.validate(token); Fork it, provide your own VT_SESSION_PRIVATE_KEY, and update the hardcoded public key. You can generate your own keys like so: import { crypto } from "https://deno.land/std@0.198.0/crypto/mod.ts";
// Generate a key pair for JWT signing and verification
const { privateKey, publicKey } = await crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
}, true, ["sign", "verify"],
);
function formatPEM(b64: string, type: "PRIVATE KEY" | "PUBLIC KEY"): string {
const lines = b64.match(/.{1,64}/g) || [];
return `-----BEGIN ${type}-----\n${lines.join("\n")}\n-----END ${type}-----`;
}
const privateKeyPem = formatPEM(
btoa(String.fromCharCode(...new Uint8Array(exportPrivateKey))),
"PRIVATE KEY",
);
const publicKeyPem = formatPEM(
btoa(String.fromCharCode(...new Uint8Array(exportPublicKey))),
"PUBLIC KEY",
);
console.log(privateKeyPem, publicKeyPem);
3
dvsj
GetWebsiteMetadata
HTTP
You know how when you paste a URL in Twitter or Slack it shows you a nice preview? This val gives you that data. Given a URL, this will return metadata about the website like title , description , imageURL , image as base64 etc. Sample input - paste this in your URL bar https://dvsj-GetWebsiteMetadata.web.val.run?targetURL=https://dvsj.in
https://dvsj-GetWebsiteMetadata.web.val.run?targetURL=<your-target-url-here> Sample output: {
status: 200,
url: "https://dvsj.in",
title: "Dav-is-here ➜",
description: "Davis' not-so-secret stash",
imgUrl: "https://www.dvsj.in/cover-picture.png",
imgData: "data:image/png;base64,qwertyblahblah"
} FAQ: Why is imgData sent when imgUrl is already present? Because you shouldn't hotlink images from 3rd parties. Store the base64 image on your server and use it in your app. It's unfair to use their server bandwidth and could be a security issue for you if they change the content of the link later.
2
zackoverflow
lisp
Script
Lispaas (lisp as a service) A mini lisp interpreter How to use: To execute code: const result = @zackoverflow.lisp(" (+ 1 2)") To just parse and return the AST: const ast = @zackoverflow.lisp("(+ 1 2)", true) The value returned is the last expression of the program, for example: const lispResult = @zackoverflow.lisp("(+ 1 2) (+ 400 20)")
console.log('Val', lispResult.val === 420) Example: Compute Fibonacci sequence let result = @zackoverflow.lisp(`
(defun fib (x)
(if (<= x 1)
x
(defun impl (i n-1 n-2)
(if (= x i)
(+ n-1 n-2)
(impl (+ i 1) (+ n-1 n-2) n-1)))
(impl 2 1 0)))
(assert-eq 0 (fib 0))
(assert-eq 1 (fib 1))
(assert-eq 1 (fib 2))
(assert-eq 2 (fib 3))
(assert-eq 3 (fib 4))
(assert-eq 5 (fib 5))
(assert-eq 8 (fib 6))
(assert-eq 13 (fib 7))
`); Documentation Functions You can define a function like so: (defun hello (x) (print x)) Rest/variadic arguments are also supported (defun variable-amount-of-args (...args) (print args))
(variable-amount-of-args "Hello" "World!") Lists Define a list like so: (let ((my-list (list 1 2 3 4)))
(print my-list)
(print (list-get my-list 1))) Internally, a list is just a Javascript array. So indexing is O(1), but that does mean cdr requires copying (vs the linked list implementation). Plists Property lists, or records. Internally these are Javascript objects. Create a plist like so: (set null :key "Value") TODO
2
maxm
openAIStreaming
HTTP
OpenAI Streaming - Assistant and Threads An example of using OpenAI to stream back a chat with an assistant. This example sends two messages to the assistant and streams back the responses when they come in. Example response: user > What should I build today?
................
assistant > Here are a few fun Val ideas you could build on Val Town:
1. **Random Joke Generator:** Fetch a random joke from an API and display it.
2. **Daily Weather Update:** Pull weather data for your location using an API and create a daily summary.
3. **Mini Todo List:** Create a simple to-do list app with add, edit, and delete functionalities.
4. **Chuck Norris Facts:** Display a random Chuck Norris fact sourced from an API.
5. **Motivational Quote of the Day:** Fetch and display a random motivational quote each day.
Which one sounds interesting to you?
user > Cool idea, can you make it even cooler?
...................
assistant > Sure, let's add some extra flair to make it even cooler!
How about creating a **Motivational Quote of the Day** app with these features:
1. **Random Color Theme:** Each day, the background color/theme changes randomly.
2. **Quote Sharing:** Add an option to share the quote on social media.
3. **Daily Notifications:** Send a daily notification with the quote of the day.
4. **User Preferences:** Allow users to choose categories (e.g., success, happiness, perseverance) for the quotes they receive.
Would you like some code snippets or guidance on implementing any of these features?
0