Skip to content

Instantly share code, notes, and snippets.

@pacoccino
Created January 10, 2022 10:03
Show Gist options
  • Save pacoccino/d65d077c057681c992d495761f5263c7 to your computer and use it in GitHub Desktop.
Save pacoccino/d65d077c057681c992d495761f5263c7 to your computer and use it in GitHub Desktop.
Fetch coingecko prices
import { serve } from "https://deno.land/std@0.120.0/http/server.ts";
async function getPrice(ids: string, vs: string) {
const url =
`https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=${vs}`;
const res = await fetch(url);
const body = res.json();
return body;
}
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
const ids = url.searchParams.get("ids") || "bitcoin";
const vs = url.searchParams.get("vs") || "usd";
const res = await getPrice(ids, vs);
return new Response(JSON.stringify(res));
}
serve(handler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment