Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Usage exmaple:
// https://username-valname.web.val.run?k=myKey&s=mySubject&m=myMessage
import { email } from "https://esm.town/v/std/email";
import { Hono } from "npm:hono@3";
const app = new Hono();
app.get("/", async (c) => {
const now = new Date();
let { to, s, m, k } = c.req.query();
s = s || "message from api";
m = m || "<empty>";
const checks = [
c.req.header("cf-ipcountry") === "US",
k === Deno.env.get("EMAIL_KEY"),
];
const failedChecks = checks.filter(el => el === false).length;
if (failedChecks > 0) {
c.status(401);
return c.text(`unauthorized - ${failedChecks}`);
}
if (m.length > 280) {
return c.text("invalid message length");
}
if (s.length > 80) {
return c.text("invalid subject length");
}
try {
await email({ to: to, subject: s, text: m });
return c.text(`message sent at ${now.toUTCString()}`);
} catch (err) {
c.status(500);
return c.text("something went wrong");
}
});
export default app.fetch;
agmm-emailapi.web.val.run
February 29, 2024