Upload a file to Public or Private IPFS
curl --request POST \
--url https://402.pinata.cloud/v1/pin/{network} \
--header 'Content-Type: application/json' \
--header 'X-PAYMENT: <api-key>' \
--data '
{
"fileSize": 123
}
'import requests
url = "https://402.pinata.cloud/v1/pin/{network}"
payload = { "fileSize": 123 }
headers = {
"X-PAYMENT": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'X-PAYMENT': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({fileSize: 123})
};
fetch('https://402.pinata.cloud/v1/pin/{network}', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://402.pinata.cloud/v1/pin/{network}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'fileSize' => 123
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-PAYMENT: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://402.pinata.cloud/v1/pin/{network}"
payload := strings.NewReader("{\n \"fileSize\": 123\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-PAYMENT", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://402.pinata.cloud/v1/pin/{network}")
.header("X-PAYMENT", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"fileSize\": 123\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://402.pinata.cloud/v1/pin/{network}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-PAYMENT"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"fileSize\": 123\n}"
response = http.request(request)
puts response.read_body{
"url": "<string>"
}x402 Services
Upload a File
Uploads a file to Pinata’s public IPFS network
POST
/
pin
/
{network}
Upload a file to Public or Private IPFS
curl --request POST \
--url https://402.pinata.cloud/v1/pin/{network} \
--header 'Content-Type: application/json' \
--header 'X-PAYMENT: <api-key>' \
--data '
{
"fileSize": 123
}
'import requests
url = "https://402.pinata.cloud/v1/pin/{network}"
payload = { "fileSize": 123 }
headers = {
"X-PAYMENT": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'X-PAYMENT': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({fileSize: 123})
};
fetch('https://402.pinata.cloud/v1/pin/{network}', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://402.pinata.cloud/v1/pin/{network}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'fileSize' => 123
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-PAYMENT: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://402.pinata.cloud/v1/pin/{network}"
payload := strings.NewReader("{\n \"fileSize\": 123\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-PAYMENT", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://402.pinata.cloud/v1/pin/{network}")
.header("X-PAYMENT", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"fileSize\": 123\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://402.pinata.cloud/v1/pin/{network}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-PAYMENT"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"fileSize\": 123\n}"
response = http.request(request)
puts response.read_body{
"url": "<string>"
}Cost
| Price | Duration |
|---|---|
| $0.10/GB * 12 | Pins for 12 months |
Example Usage
In order to access these endpoints you will need to use either@x402/axios or @x402/fetch. Once installed you will also need either Viem or a Coinbase developer account. From there you can create an account locally or through the CDP Wallet API.
When you make a request to one of the Pinata x402 endpoints it will return a 402 error saying payment is required. Then the fetchWithPayment method from the fetch or axios library will make a second requst for the requested payment amount. After payment is settled then you can use the returned presigned URL to upload the file to Pinata.
import { wrapFetchWithPayment, decodeXPaymentResponse } from "@x402/fetch";
import { account } from "./viem";
const fetchWithPayment = wrapFetchWithPayment(fetch, account);
const url = "https://402.pinata.cloud/v1/pin/public";
fetchWithPayment(url, {
method: "POST",
body: JSON.stringify({
fileSize: 5000000,
}),
})
.then(async (response) => {
const body = (await response.json()) as { url: string };
console.log(body);
const uuid = crypto.randomUUID();
const file = new File([`Paid and pinned by 402.pinata.cloud: ${uuid}`], "file.txt");
const data = new FormData();
data.append("network", "public");
data.append("file", file);
const uploadReq = await fetch(body.url, {
method: "POST",
body: data,
});
const uploadRes = await uploadReq.json();
console.log(uploadRes);
})
.catch((error) => {
console.error(error.response?.data?.error);
});
https://ipfs.io/ipfs/:CID. If you upload a file as private then it will not be accessible on public IPFS, so in order to access it you need to create a temporary access URL. This flow is similar to the previous one, except you would provide the CID that you uploded previously that you would like to access. After a successful payment the server will return a URL you can access the file with.
import { wrapFetchWithPayment, decodeXPaymentResponse } from "@x402/fetch";
import { account } from "./viem";
const fetchWithPayment = wrapFetchWithPayment(fetch, account);
const url =
"https://402.pinata.cloud/v1/retrieve/private/bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7garjiubll2ceym4";
fetchWithPayment(url, {
method: "GET",
})
.then(async (response) => {
const body = (await response.json()) as { url: string };
console.log(body);
})
.catch((error) => {
console.error(error.response?.data?.error);
});
Authorizations
Base64 encoded x402 payment payload
Path Parameters
Upload to either public or private IPFS network
Available options:
public, private Body
application/json
Size of the file to be uploaded in bytes
Response
Successful operation
The signed URL for file upload