returns image URL (and only image URL) for a given NFT contract + tokenId. Uses Alchemy's NFT API

to this use val, copy the Web API endpoint and use ?query params to specify the contract address and tokenId you want:

https://jamiedubs-nftimage.web.val.run/?contractAddress=0x3769c5700Da07Fe5b8eee86be97e061F961Ae340&tokenId=666 - FIXME valtown is turning & into "&", you need to fix it. even like this broken

plain text by default. for JSON add &format=json, for an <img> tag use &format=html

for other NFT metadata: https://www.val.town/v/jamiedubs.nftMetadata

Readme
Fork
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
import { fetchNftMetadata } from "https://esm.town/v/jamiedubs/nftMetadata";
export const nftImage = async (req: Request) => {
const searchParams = new URL(req.url).searchParams;
const contractAddress = searchParams.get("contractAddress");
const tokenId = searchParams.get("tokenId");
const format = searchParams.get("format")?.toLowerCase() ?? "text";
console.log("nftImage", { contractAddress, tokenId });
const json = await fetchNftMetadata(contractAddress, tokenId);
console.log("nftMetadata response =>", json);
const imageUrl = json["metadata"] && json["metadata"]["image"];
if (format == "json") {
return Response.json({ imageUrl });
}
else if (format == "html") {
return new Response(`<img src="${imageUrl}"/>`, {
headers: {
"Content-Type": "text/html",
},
});
}
else {
return new Response(imageUrl, {
headers: {
"Content-Type": "text/plain",
},
});
}
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.