Runs every 7 days
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
import { email } from "https://esm.town/v/std/email";
// 4.1%
const maximumMortageRate = 4.1;
const fredApiKey = Deno.env.get("FRED_API_SECRET_KEY");
const fredApiUrl =
`https://api.stlouisfed.org/fred/series/observations?series_id=MORTGAGE30US&api_key=${fredApiKey}&file_type=json&sort_order=desc&limit=1`;
export default async function main(req: Request) {
try {
const response = await fetch(fredApiUrl);
const data = await response.json();
if (data.observations && data.observations.length > 0) {
const latestRate = data.observations[0];
const rate = parseFloat(latestRate.value);
if (rate <= maximumMortageRate) {
const date = new Date(latestRate.date).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
const htmlResponse = `
<html>
<body>
<h1>๐Ÿ“Š Current 30-Year Fixed Rate Mortgage Average</h1>
<p>As of ${date}, the rate is: <strong>${rate.toFixed(2)}%</strong></p>
</body>
</html>
`;
await email({ subject: `Mortage rates dropped to ${rate.toFixed(2)}!`, html: htmlResponse });
}
} else {
await email({ subject: "No data available" });
}
} catch (error) {
await email({ subject: "Error fetching mortgage rates", text: error.message });
}
}