Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

This val shows how you can use the new dub.links.upsert method to shorten a URL using the Dub API without creating duplicate links in your workspace for the given URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
import { Dub } from "https://esm.sh/dub";
// import { Dub } from "dub"; <-- use this when using with a Node backend
const dub = new Dub({
token: Deno.env.get("DUB_API_KEY"), // optional – defaults to process.env.DUB_API_KEY (Learn more: https://d.to/tokens)
});
export default async function(req: Request): Promise<Response> {
const { shortLink } = await dub.links.upsert({
url: "https://www.val.town/v/steventey/dubLinksUpsert",
});
return Response.json({ shortLink });
}
steventey-dublinksupsert.web.val.run
June 28, 2024