Upload to IPFS with nft.storage

Problem

You want to upload images / assets to IPFS network w/o running your own node

Solution

There are some paid services like pinata.cloud. And fortunately I found the free service called nft.storage that provide free API.

Steps

1, Go to nft.storage , register an account to get an API token. It is free

2, Upload to it with normal HTTP Post request.

Examples

Use curl:

1
curl -X POST --data-binary @art.jpg -H 'Authorization: Bearer YOUR_API_KEY' https://api.nft.storage/upload

it will return format like this:

1
2
3
4
{
"ok": true,
"value": { "cid": "bafy..." }
}

The cid is your uploaded asset ID in IPFS, you can visit it with many IPFS gateways, like:

1
2
3
https://ipfs.io/ipfs/<cid>
https://<ipfs>.ipfs.dweb.link
https://cloudflare-ipfs.com/ipfs/<cid>

Use Golang:

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
42
43
44
45
46
47
48
package main

import (
"context"
"fmt"
"io/ioutil"
"log"

"github.com/carlmjohnson/requests"
)

const (
API_URL = "https://api.nft.storage/upload"
API_KEY = "<API Key>"
ENDPOINT = "https://%s.ipfs.dweb.link"
)

type RespValue struct {
CID string `json:"cid"`
}

type Resp struct {
OK bool `json:"ok"`
Value RespValue `json:"value"`
}

func main() {
fileBytes, err := ioutil.ReadFile("./cat.png")
if err != nil {
log.Fatal(err)
}

resp := Resp{}
authToken := fmt.Sprintf("Bearer %s", API_KEY)
err = requests.URL(API_URL).
BodyBytes(fileBytes).
ToJSON(&resp).
Header("Authorization", authToken).
Fetch(context.Background())

if err != nil {
log.Fatal(err)
}

cid := resp.Value.CID
url := fmt.Sprintf(ENDPOINT, cid)
fmt.Printf("Uploaded: %s\n", url)
}

This will upload a image named cat.png to IPFS network:

1
2
$ go run main.go
Uploaded: https://bafybeigp4zbuq2xm5mc4n4oyfkgntyq3ooxb4vqrbguq7hmvbfeb2nh3dq.ipfs.dweb.link

You can view this image here from this link

References: