Val Town Code SearchReturn to Val Town

API Access

You can access search results via JSON API by adding format=json to your query:

https://codesearch.val.run/$%7Bsuccess?q=function&page=1&format=json

For typeahead suggestions, use the /typeahead endpoint:

https://codesearch.val.run/typeahead?q=function

Returns an array of strings in format "username" or "username/projectName"

Found 18010 results for "function"(1001ms)

Upgrade to Deno633 words

https://docs.val.town/upgrading/upgrade-to-deno/
User-defined functions can be synchronous 🥳 In the old runtime, you would need to await the call to any @user.function. That is no longer the case! Now only async functions

Sections

User-defined functions can be synchronous 🥳

User-defined functions can be synchronous 🥳 In the old runtime, you would need to await the call to any @user.function. That is no longer the case! Now only async functions

setTimeout has been removed

setTimeout has been removed. Contact us if you need this functionality.

Promises are not recursively resolved between functions

recursively resolved between functions. In the prior runtime, all values were recursively awaited. This means that if you returned an array or object with a Promise nested somewhere inside it,

Cron evaluations has 0 arguments where it used to have 1

purple). This does not affect that fact that crons pass the Interval object to the function that has been scheduled, which allows you to get the lastRunAt value of the

Express to HTTP migration325 words

https://docs.val.town/troubleshooting/express-to-http-migration/
for the express versus HTTP types: // Express handler. export function handler(req, res) { res.send("Hello world"); } // HTTP handler. export function handler(req) { return new Response("Hello world"); } The

Sections

Parameters

for the express versus HTTP types: // Express handler. export function handler(req, res) { res.send("Hello world"); } // HTTP handler. export function handler(req) { return new Response("Hello world"); }

The response object

other details by chaining functions off of the response object, with the HTTP type, these are options you set for the Response object. // Express handler. export function handler(req, res)

The request object

string parameters, will require different code: // Express handler. export function handler(req, res) { res.send(req.query.name); } // HTTP handler. export function handler(req) { return new Response(new URL(req.url).searchParams.get("name")); }

Exports121 words

https://docs.val.town/troubleshooting/exports/
the function to run when that val is triggered. If your val has multiple exports, then we require one of them to the default export, which will be the function

Sections

Exports

the function to run when that val is triggered. If your val has multiple exports, then we require one of them to the default export, which will be the function

HTTP99 words

https://docs.val.town/vals/http/
Peko. These handlers need to export a function that takes a Request object as the first parameter and returns a Response object. The function can be asynchronous. Basic examples Routing

Sections

HTTP

Peko. These handlers need to export a function that takes a Request object as the first parameter and returns a Response object. The function can be asynchronous. Basic examples Routing

Save HTML form data356 words

https://docs.val.town/guides/save-html-form-data/
web browser, the server (your val function) gets sent a GET request. You can check the HTTP method using req.method and change how your val function responds. See Web forms

Sections

Create an HTTP trigger

Create an HTTP trigger. Write a val function that accepts a Request and returns a Response.

Host your form on Val Town

web browser, the server (your val function) gets sent a GET request. You can check the HTTP method using req.method and change how your val function responds. See Web forms

Early Return456 words

https://docs.val.town/vals/http/early-return/
set up a queue. In your early-returning HTTP file: early-returning.tsRun in Val Town ↗ async function handle(request: Request) { // Send off the relevant data a queue HTTP file. //

Sections

How to set up a queue

early-returning HTTP file: early-returning.tsRun in Val Town ↗ async function handle(request: Request) { // Send off the relevant data a queue HTTP file. // This `fetch` is not awaited. fetch("https://my-queue.web.val.run",

Promises should otherwise be awaited

- besides this narrow use-case, errors that occur in promises won’t be properly handled and functions may run out-of-order. For example, if you use fetch to request some resource, but

Your first scheduled cron602 words

https://docs.val.town/quickstarts/first-cron/
The default code will look like this: weatherNotifier.ts export default async function (interval: Interval) { // your code… } This function will be run on a schedule. By default, this

Sections

Step 2: Set up a Cron Trigger

The default code will look like this: weatherNotifier.ts export default async function (interval: Interval) { // your code… } This function will be run on a schedule. By default, this

Step 3: Get the weather

Step 3Run in Val Town ↗ import { getWeather } from "https://esm.town/v/stevekrouse/getWeather"; export default async function (interval: Interval) { let weather = await getWeather("Brooklyn, NY"); console.log(weather.current_condition[0].FeelsLikeF); } Replace Brooklyn, NY

Step 4: Send yourself an email

import { email } from "https://esm.town/v/std/email"; import { getWeather } from "https://esm.town/v/stevekrouse/getWeather"; export default async function (interval: Interval) { let weather = await getWeather("Brooklyn, NY"); let feelsLike = weather.current_condition[0].FeelsLikeF; let

Next steps

updates to Discord. weather_forecast_in_the_morning - weather forecast on Telegram. weatherBot - OpenAI Weather Bot via function calling. aqi - email alerts when AQI is unhealthy near you. …add yours here!

CORS356 words

https://docs.val.town/troubleshooting/cors/
any default headers. Example: Custom CORS Configuration. Custom CorsRun in Val Town ↗ export async function myEndpoint() { return new Response("Hello", { headers: { "Access-Control-Allow-Origin": "https://specific-domain.com", "Access-Control-Allow-Methods": "GET,POST", }, });

Sections

Example: Custom CORS Configuration

Example: Custom CORS Configuration. Custom CorsRun in Val Town ↗ export async function myEndpoint() { return new Response("Hello", { headers: { "Access-Control-Allow-Origin": "https://specific-domain.com", "Access-Control-Allow-Methods": "GET,POST", }, }); }

Example: Handling Preflight Requests

Example: Handling Preflight Requests. For complete control over CORS behavior, you can handle OPTIONS requests explicitly: Handle Preflight Request export async function myEndpoint(req) { if (req.method === "OPTIONS") { return

Removing CORS Headers

Removing CORS Headers. If we detect that you’ve set your own "Access-Control-Allow-Origin" header we won’t add any custom CORS headers to your request. Remove CORS Headers export async function myEndpoint()

Proxied fetch207 words

https://docs.val.town/std/fetch/
contains an alternative version, std/fetch, that wraps the JavaScript Fetch API to provide additional functionality. The fetch function from std/fetch reroutes requests using a proxy vendor so that requests obtain

Sections

Proxied fetch

contains an alternative version, std/fetch, that wraps the JavaScript Fetch API to provide additional functionality. The fetch function from std/fetch reroutes requests using a proxy vendor so that requests obtain

Migrating Deprecated HTTP Vals541 words

https://docs.val.town/troubleshooting/migrating-deprecated-http-vals/
return a different random number on each request: const randomValue = Math.random(); export default async function (req: Request): Promise<Response> { return Response.json({ randomValue }); } Run in Val Town. Terminal

Sections

Accidentally re-using values.

return a different random number on each request: const randomValue = Math.random(); export default async function (req: Request): Promise<Response> { return Response.json({ randomValue }); } Run in Val Town. Terminal

Intentionally caching values for performance!

data fetching. const expensiveData = await fetchLargeDataset(); const cache = new Map(); export default async function (req: Request): Promise<Response> { const url = new URL(req.url); const key = url.searchParams.get("key"); if

Migration Checklist

be safely cached. Move variables that need to be unique per request inside the handler function. Consider opportunities to improve performance by intentionally caching expensive computations or initializations. Test your

x-cache-bugserver.ts1 match

@hacksore•Updated 42 mins ago
1export default function server(request: Request): Response {
2 // Define the redirect targets
3 const SPECIAL_REDIRECT_TARGET = "https://apple.com/apple-events";

pondiverseupdateTable1 match

@argmn•Updated 1 hour ago
2
3export const TABLE_NAME = "pondiverse_creations_v4";
4export default async function(req: Request): Promise<Response> {
5 await sqlite.execute(
6 `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (

getFileEmail4 file matches

@shouser•Updated 1 week ago
A helper function to build a file's email
tuna

tuna8 file matches

@jxnblk•Updated 1 week ago
Simple functional CSS library for Val Town
webup
LangChain (https://langchain.com) Ambassador, KubeSphere (https://kubesphere.io) Ambassador, CNCF OpenFunction (https://openfunction.dev) TOC Member.
lost1991
import { OpenAI } from "https://esm.town/v/std/openai"; export default async function(req: Request): Promise<Response> { if (req.method === "OPTIONS") { return new Response(null, { headers: { "Access-Control-Allow-Origin": "*",