Make requests

cURL

cURL generates the auth header and encodes your credentials behind the scenes.
Include the -u flag with the authentication information.
1
curl -X POST -F file=@myfile -u "PROJECT_ID:PROJECT_SECRET" "https://ipfs.infura.io:5001/api/v0/add"
Copied!

JavaScript

Wrap JavaScript calls to IPFS with the Infura Authorization header.
1
xhr.setRequestHeader("Authorization", "Basic " + btoa(PROJECT_ID + ":" + PROJECT_SECRET));
Copied!

NodeJS

Change the projectId and projectSecret in the NodeJS example code below.
Save the following script to a file, e.g. index.js.
1
const https = require('https');
2
3
const projectId = '1qmt...XXX';
4
const projectSecret = 'c920...XXX';
5
6
const options = {
7
host: 'ipfs.infura.io',
8
port: 5001,
9
path: '/api/v0/pin/add?arg=QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn',
10
method: 'POST',
11
auth: projectId + ':' + projectSecret,
12
};
13
14
let req = https.request(options, (res) => {
15
let body = '';
16
res.on('data', function (chunk) {
17
body += chunk;
18
});
19
res.on('end', function () {
20
console.log(body);
21
});
22
});
23
req.end();
Copied!
In a terminal window, run the script with node index.js
Output something like:
1
{"Pins":["QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn"]}
Copied!

Python

Change the projectId and projectSecret in the Python example code below.
Save the following script to a file, e.g. index.py.
1
import requests
2
3
projectId = "<ADD PROJECT ID HERE>"
4
projectSecret = "<ADD PROJECT SECRET HERE>"
5
endpoint = "https://ipfs.infura.io:5001"
6
7
### CREATE AN ARRAY OF TEST FILES ###
8
files = {
9
'file': 'myNFT.png'
10
}
11
12
### ADD FILE TO IPFS AND SAVE THE HASH ###
13
response1 = requests.post(endpoint + '/api/v0/add', files=files, auth=(projectId, projectSecret))
14
print(response1)
15
hash = response1.text.split(",")[1].split(":")[1].replace('"','')
16
print(hash)
17
18
### READ FILE WITH HASH ###
19
params = {
20
'arg': hash
21
}
22
response2 = requests.post(endpoint + '/api/v0/cat', params=params, auth=(projectId, projectSecret))
23
print(response2)
24
print(response2.text)
25
26
### REMOVE OBJECT WITH PIN/RM ###
27
response3 = requests.post(endpoint + '/api/v0/pin/rm', params=params, auth=(projectId, projectSecret))
28
print(response3.json())
Copied!
Run the script with python index.py.
Output something like:
1
<Response [200]>
2
QmWtBbpKST49AQFLx8HAdwwjUu7HBP2wrtAH1x8df5qrVm
3
<Response [200]>
4
myNFT.png
5
{'Pins': ['QmWtBbpKST49AQFLx8HAdwwjUu7HBP2wrtAH1x8df5qrVm']}
Copied!

ipfs-http-client

Use the official ipfs-http-client JavaScript client library.
Install the library with npm install --save ipfs-http-client.
Save the following script to a file, e.g. index.js.
1
const ipfsClient = require('ipfs-http-client');
2
3
const projectId = '1qmt...XXX';
4
const projectSecret = 'c920...XXX';
5
const auth =
6
'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64');
7
8
const client = ipfsClient.create({
9
host: 'ipfs.infura.io',
10
port: 5001,
11
protocol: 'https',
12
headers: {
13
authorization: auth,
14
},
15
});
16
17
client.pin.add('QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn').then((res) => {
18
console.log(res);
19
});
Copied!
Run with node index.js.
Output something like:
1
CID(QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn)
Copied!

go-ipfs-api

  • Use the official IPFS go-ipfs-api GoLang API.
  • Install with go get -u github.com/ipfs/go-ipfs-api.
  • Create a go module with go mod init infura.
  • Save the following script to a file, e.g. index.go, and include the Infura auth header with the http.RoundTripper wrapper.
1
package main
2
3
import (
4
"fmt"
5
"net/http"
6
"os"
7
"strings"
8
9
ipfsApi "github.com/ipfs/go-ipfs-api" // v0.2.0
10
)
11
12
func main() {
13
projectId := "1qmt...XXX"
14
projectSecret := "c920...XXX"
15
16
shell := ipfsApi.NewShellWithClient("https://ipfs.infura.io:5001", NewClient(projectId, projectSecret))
17
cid, err := shell.Add(strings.NewReader("Infura IPFS - Getting started demo."))
18
if err != nil {
19
fmt.Println(err)
20
os.Exit(1)
21
}
22
23
fmt.Printf("Data successfully stored in IPFS: %v\n", cid)
24
}
25
26
// NewClient creates an http.Client that automatically perform basic auth on each request.
27
func NewClient(projectId, projectSecret string) *http.Client {
28
return &http.Client{
29
Transport: authTransport{
30
RoundTripper: http.DefaultTransport,
31
ProjectId: projectId,
32
ProjectSecret: projectSecret,
33
},
34
}
35
}
36
37
// authTransport decorates each request with a basic auth header.
38
type authTransport struct {
39
http.RoundTripper
40
ProjectId string
41
ProjectSecret string
42
}
43
44
func (t authTransport) RoundTrip(r *http.Request) (*http.Response, error) {
45
r.SetBasicAuth(t.ProjectId, t.ProjectSecret)
46
return t.RoundTripper.RoundTrip(r)
47
}
Copied!
Run with go run index.go.
Output something like:
1
CID(QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn)
Copied!

go-ipfs-http-client

  • Use the official IPFS go-ipfs-http-client GoLang API.
  • Install with go get github.com/ipfs/go-ipfs-http-client.
  • Create a go module with go mod init infura.
  • Save the following script to a file, e.g. index.go, and include the Infura auth header with the http.RoundTripper wrapper.
1
package main
2
3
import (
4
"context"
5
"encoding/base64"
6
"fmt"
7
"net/http"
8
"os"
9
"strings"
10
11
ipfsFiles "github.com/ipfs/go-ipfs-files" // v0.0.8
12
ipfsApi "github.com/ipfs/go-ipfs-http-client" // v0.1.0
13
)
14
15
func main() {
16
projectId := "1qmt...XXX"
17
projectSecret := "c920...XXX"
18
19
httpClient := &http.Client{}
20
httpApi, err := ipfsApi.NewURLApiWithClient("https://ipfs.infura.io:5001", httpClient)
21
if err != nil {
22
fmt.Println(err)
23
os.Exit(1)
24
}
25
httpApi.Headers.Add("Authorization", "Basic " + basicAuth(projectId, projectSecret))
26
27
content := strings.NewReader("Infura IPFS - Getting started demo.")
28
p, err := httpApi.Unixfs().Add(context.Background(), ipfsFiles.NewReaderFile(content))
29
if err != nil {
30
fmt.Println(err)
31
os.Exit(1)
32
}
33
34
fmt.Printf("Data successfully stored in IPFS: %v\n", p.Cid().String())
35
}
36
37
func basicAuth(projectId, projectSecret string) string {
38
auth := projectId + ":" + projectSecret
39
return base64.StdEncoding.EncodeToString([]byte(auth))
40
}
Copied!
Run with go run index.go.
Output something like:
1
Data successfully stored in IPFS: QmTHr95iiwSTA2USxx4g5kKnhqsNRixqohhwxjvdXmSrWn
Copied!