jamiedubs-nftmetadata.web.val.run
Readme

use by copying web API endpoint and appending "?contractAddress=...&tokenId..." - like this

uses Alchemy for indexed NFT data: https://docs.alchemy.com/reference/getnftmetadata

plus it's using my personal API key. don't abuse this or I'll disable it! yeesh

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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export async function fetchNftMetadata(contractAddress: string, tokenId: string) {
const apiKey = process.env.ALCHEMY_API_KEY;
const rpcUrl = `https://eth-mainnet.g.alchemy.com/nft/v2/${apiKey}`;
const url = `${rpcUrl}/getNFTMetadata?contractAddress=${contractAddress}&tokenId=${tokenId}`;
const response = await fetch(url, {
method: "GET",
headers: {
"accept": "application/json",
},
});
console.log("Alchemy response", response);
if (!response) {
return { error: "no response from Alchemy" };
} else if (!response.ok) {
return { error: `failed with status: ${response.statusText}` };
}
else {
const json = await response.json();
console.log("response OK", json);
return json;
}
}
export default async function nftMetadata(req: Request): Promise<Response> {
const searchParams = new URL(req.url).searchParams;
const contractAddress = searchParams.get("contractAddress");
const tokenId = searchParams.get("tokenId");
const format = searchParams.get("format")?.toLowerCase() ?? "text";
if (!contractAddress || !tokenId) {
return Response.json({ error: "you must specify ?contractAddress...&tokenId=..." });
}
const json = await fetchNftMetadata(contractAddress, tokenId);
return Response.json(json);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v18
December 7, 2023