Web3 Blog

Create a web3 blog where your readers can collect blog posts as NFTs
The Underdog API makes it easy to build your own web3 blog. Using the API, you can:
  • Create Collections for each of your blog posts
  • Allow your readers to collect blog posts as NFTs
You can fork an open-source example of a web3 blog to quickly get started. You'll find details on how to set up the repo in the README.
In the guide, I'll walk through the three pieces of the repo that use the Underdog API to create NFTs for blog posts.

Next.js API Endpoint

In pages/api/nfts.ts, we use two API endpoints
  1. 1.
    GET /v1/nfts to get information if the currently connected wallet has an NFT in the blog post's Collection
  2. 2.
    POST /v1/nfts to create an NFT in the Collection when a reader collects the blog post
pages/api/nfts.ts
1
import { UNDERDOG_API_KEY, UNDERDOG_API_URL } from "@/lib/constants"
2
import { metadataImageUrl } from "@/lib/underdog"
3
import axios from "axios"
4
import { NextApiRequest, NextApiResponse } from "next"
5
6
export default async function handler(
7
req: NextApiRequest,
8
res: NextApiResponse
9
) {
10
try {
11
if (req.method === "GET") {
12
const response = await axios.get(
13
`${UNDERDOG_API_URL}/v1/nfts?ownerAddress=${req.query.ownerAddress}&collectionAddress=${req.query.collectionAddress}`,
14
{
15
headers: {
16
"Content-Type": "application/json",
17
Authorization: `Bearer ${UNDERDOG_API_KEY}`,
18
},
19
}
20
)
21
22
res.status(200).json(response.data)
23
}
24
25
if (req.method === "POST") {
26
const response = await axios.post(
27
`${UNDERDOG_API_URL}/v1/nfts`,
28
{
29
name: req.body.name,
30
description: req.body.description,
31
image: metadataImageUrl(req.body.collectionAddress),
32
collectionAddress: req.body.collectionAddress,
33
ownerAddress: req.body.ownerAddress,
34
},
35
{
36
headers: {
37
"Content-Type": "application/json",
38
Authorization: `Bearer ${UNDERDOG_API_KEY}`,
39
},
40
}
41
)
42
43
res.status(200).json(response.data)
44
}
45
} catch (e: any) {
46
console.log(e)
47
res.status(400).json({ error: e.message })
48
}
49
}

Collect Button

Within each blog post, we have a CollectButton component that mints the NFT directly to the reader's wallet.
The key part of the component is the handleCollect function where we take the blog post metadata (title, description, and Collection address) and send that data to our Next.js API endpoint we just created.
components/CollectButton/index.tsx
1
...
2
const handleCollect = async () => {
3
if (publicKey) {
4
toggleCollecting()
5
await axios.post("/api/nfts", {
6
name: entry.title,
7
description: entry.description,
8
collectionAddress: entry.collectionAddress,
9
ownerAddress: publicKey?.toString(),
10
})
11
await refreshNfts()
12
toggleCollecting()
13
}
14
}
15
...

Markdown Blog Posts

Within each blog post, we need to include information about our blog post as well as the Collection we want the blog post NFTs to be a part of.
pages/your-blog-post/index.mdx
export const entry = {
title: "Your Blog Post",
description: "Add a description for your blog post",
collectionAddress: "E6H1MYDiFhUkmAWPnxTMS9iEPpJtvMU6za5pvqUt6Hm3"
}
Within the Underdog dashboard, you can create a new Collection for each blog post and upload unique artwork for each NFT.