Pinata Docs
Search…
Pinata NodeJS SDK

Pinata SDK

Official NodeJS SDK for Pinata

Overview

The Pinata NodeJS SDK provides the quickest / easiest path for interacting with the Pinata API.

Installation

1
npm install --save @pinata/sdk
Copied!

Setup

To start, simply require the Pinata SDK and set up an instance with your Pinata API Keys. Don't know what your keys are? Check out your Account Page.
1
const pinataSDK = require('@pinata/sdk');
2
const pinata = pinataSDK('yourPinataApiKey', 'yourPinataSecretApiKey');
Copied!
Quickly test that you can connect to the API with the following call:
1
pinata.testAuthentication().then((result) => {
2
//handle successful authentication here
3
console.log(result);
4
}).catch((err) => {
5
//handle error here
6
console.log(err);
7
});
Copied!

Usage

Once you've set up your instance, using the Pinata SDK is easy. Simply call your desired function and handle the results of the promise.

hashMetadata

Allows the user to change the name and keyvalues associated with content pinned to Pinata. Changes made via this endpoint only affect the metadata for the hash passed in. Metadata is specific to Pinata and does not modify the actual content stored on IPFS in any way. It is simply a convenient way of keeping track of what content you have stored.
pinata.hashMetadata(ipfsPinHash, metadata)
Params
  • ipfsPinHash - A string for a valid IPFS Hash that you have pinned on Pinata.
  • metadata A JSON object containing the following:
    • name (optional) - A new name that Pinata will associate with this particular hash.
    • keyvalues (optional) - A JSON object with the updated keyvalues you want associated with the hash provided (see more below)
Adding or modifying keyvalues
To add or modify existing keyvalues, simply provide them in the following format for the keyvalues object:
1
keyvalues: {
2
newKey: 'newValue', //this adds a keyvalue pair
3
existingKey: 'newValue' //this modifies the value of an existing key if that key already exists
4
}
Copied!
Removing keyvalues
To remove a keyvalue pair, simply provide null as the value for an existing key like so:
1
keyvalues: {
2
existingKeyToRemove: null //this removes a keyvalue pair
3
}
Copied!
Response
If the operation is successful, you will receive back an "OK" REST 200 status.
Example Code
1
const metadata = {
2
name: 'new custom name',
3
keyvalues: {
4
newKey: 'newValue',
5
existingKey: 'newValue',
6
existingKeyToRemove: null
7
}
8
};
9
pinata.hashMetadata('yourHashHere', metadata).then((result) => {
10
//handle results here
11
console.log(result);
12
}).catch((err) => {
13
//handle error here
14
console.log(err);
15
});
Copied!

hashPinPolicy

Allows the user to change the pin policy for an individual piece of content. Changes made via this endpoint only affect the content for the hash passed in. They do not affect a user's account level pin policy.
To read more about pin policies, please check out the Regions and Replications Documentation.
pinata.hashPinPolicy(ipfsPinHash, newPinPolicy)
Params
  • ipfsPinHash - A string for a valid IPFS Hash that you have pinned on Pinata.
  • newPinPolicy A JSON object with a new Pin Policy for the hash.
Response
If the operation is successful, you will receive back an "OK" REST 200 status.
Example Code
1
const newPinPolicy = {
2
regions: [
3
{
4
id: 'FRA1',
5
desiredReplicationCount: 2
6
},
7
{
8
id: 'NYC1',
9
desiredReplicationCount: 2
10
}
11
]
12
}
13
};
14
pinata.hashPinPolicy('yourHashHere', newPinPolicy).then((result) => {
15
//handle results here
16
console.log(result);
17
}).catch((err) => {
18
//handle error here
19
console.log(err);
20
});
Copied!

pinByHash

Adds a hash to Pinata's pin queue to be pinned asynchronously.
pinata.pinByHash(hashToPin, options)
Params
  • hashToPin - A string for a valid IPFS Hash (Also known as a CID)
  • options (optional): A JSON object that can contain following keyvalues:
    • pinataMetadata (optional): A JSON object with optional metadata for the hash being pinned
    • pinataOptions
      • hostNodes (optional): An array of multiaddresses for nodes that are currently hosting the content to be pinned
      • customPinPolicy A JSON object with a new Pin Policy for the hash.
Response
1
{
2
id: This is Pinata's ID for the pin job,
3
ipfsHash: This is the IPFS multi-hash provided to Pinata to pin,
4
status: The current status of the pin job. If the request was successful the status should be 'searching'.
5
name: The name of the pin (if provided initially)
6
}
Copied!
Example Code
1
const options = {
2
pinataMetadata: {
3
name: MyCustomName,
4
keyvalues: {
5
customKey: 'customValue',
6
customKey2: 'customValue2'
7
}
8
},
9
pinataOptions: {
10
hostNodes: [
11
'/ip4/hostNode1ExternalIP/tcp/4001/ipfs/hostNode1PeerId',
12
'/ip4/hostNode2ExternalIP/tcp/4001/ipfs/hostNode2PeerId'
13
],
14
customPinPolicy: {
15
regions: [
16
{
17
id: 'FRA1',
18
desiredReplicationCount: 1
19
},
20
{
21
id: 'NYC1',
22
desiredReplicationCount: 2
23
}
24
]
25
}
26
}
27
};
28
pinata.pinByHash('yourHashHere', options).then((result) => {
29
//handle results here
30
console.log(result);
31
}).catch((err) => {
32
//handle error here
33
console.log(err);
34
});
Copied!

pinFileToIPFS

Send a file to Pinata for direct pinning to IPFS.
pinata.pinFileToIPFS(readableStream, options)
Params
  • readableStream - A readableStream of the file to be added
  • options (optional): A JSON object that can contain the following keyvalues:
    • pinataMetadata (optional): A JSON object with optional metadata for the file being pinned
    • pinataOptions (optional): A JSON object with additional options for the file being pinned
Response
1
{
2
IpfsHash: This is the IPFS multi-hash provided back for your content,
3
PinSize: This is how large (in bytes) the content you just pinned is,
4
Timestamp: This is the timestamp for your content pinning (represented in ISO 8601 format)
5
}
Copied!
Example Code
1
const fs = require('fs');
2
const readableStreamForFile = fs.createReadStream('./yourfile.png');
3
const options = {
4
pinataMetadata: {
5
name: MyCustomName,
6
keyvalues: {
7
customKey: 'customValue',
8
customKey2: 'customValue2'
9
}
10
},
11
pinataOptions: {
12
cidVersion: 0
13
}
14
};
15
pinata.pinFileToIPFS(readableStreamForFile, options).then((result) => {
16
//handle results here
17
console.log(result);
18
}).catch((err) => {
19
//handle error here
20
console.log(err);
21
});
Copied!

pinFromFS

Read from a location on your local file system and recursively pin the contents to IPFS (node.js only).
Both individual files, as well as directories can be read from.
pinata.pinFromFS(sourcePath, options)
Params
  • sourcePath - The location on your local filesystem that should be read from.
  • options (optional): A JSON object that can contain the following keyvalues:
    • pinataMetadata (optional): A JSON object with optional metadata for the file being pinned
    • pinataOptions (optional): A JSON object with additional options for the file being pinned
Response
1
{
2
IpfsHash: This is the IPFS multi-hash provided back for your content,
3
PinSize: This is how large (in bytes) the content you just pinned is,
4
Timestamp: This is the timestamp for your content pinning (represented in ISO 8601 format)
5
}
Copied!
Example Code
1
const sourcePath = '/Users/me/builds/my-awesome-website/';
2
const options = {
3
pinataMetadata: {
4
name: 'My Awesome Website',
5
keyvalues: {
6
customKey: 'customValue',
7
customKey2: 'customValue2'
8
}
9
},
10
pinataOptions: {
11
cidVersion: 0
12
}
13
};
14
pinata.pinFromFS(sourcePath, options).then((result) => {
15
//handle results here
16
console.log(result);
17
}).catch((err) => {
18
//handle error here
19
console.log(err);
20
});
Copied!

pinJobs

This endpoint allows users to search for the status of all hashes that are currently in Pinata's pin queue. Records in the pin queue arrived there through either the pinByHash operation or by failing during a pinHashToIPFS operation.
pinata.pinJobs(filters)
Params
  • filters (optional): An object that can consist of the following optional query parameters:
    • sort (optional): How you wish for the records in the response to be sorted. Valid inputs for this are:
      • 'ASC'
      • 'DESC'
    • status (optional): What the current status of the record is in the pin queue. Valid statuses and their meanings are:
      • prechecking - Pinata is running preliminary validations on your pin request.
      • searching - Pinata is actively searching for your content on the IPFS network. This may take some time if your content is isolated.
      • retrieving - Pinata has located your content and is now in the process of retrieving it.
      • expired - Pinata wasn't able to find your content after a day of searching the IPFS network. Please make sure your content is hosted on the IPFS network before trying to pin again.
      • over_free_limit - Pinning this object would put you over the free tier limit. Please add a credit card to continue pinning content.
      • over_max_size - This object is too large of an item to pin. If you're seeing this, please contact us for a more custom solution.
      • invalid_object - The object you're attempting to pin isn't readable by IPFS nodes. Please contact us if you receive this, as we'd like to better understand what you're attempting to pin.
      • bad_host_node - The provided host node(s) were either invalid or unreachable. Please make sure all provided host nodes are online and reachable.
    • ipfs_pin_hash (optional): A string for a valid IPFS hash (also known as a CID) to search for
    • limit (optional): Limit the amount of results returned per page of results (default is 5, and max is also 1000)
    • offset (optional): Provide the record offset for records being returned. This is how you retrieve records on additional pages (default is 0)
Response
1
{
2
count: (this is the total number of pin job records that exist for the query filters you passed in),
3
rows: [
4
{
5
id: (the id for the pin job record),
6
ipfs_pin_hash: (the IPFS multi-hash for the content you pinned),
7
date_queued: (The date this hash was initially queued to be pinned - represented in ISO 8601 format),
8
name: (If you passed in a name for your hash, it will be listed here),
9
status: (The current status for the pin job)
10
},
11
{
12
same record format as above
13
}
14
.
15
.
16
.
17
]
18
}
Copied!
Example Code
1
const filters = {
2
sort: 'ASC',
3
status: 'searching',
4
ipfs_pin_hash: 'Qma6e8dovfLyiG2UUfdkSHNPAySzrWLX9qVXb44v1muqcp',
5
limit: 10,
6
offset: 0
7
};
8
pinata.pinJobs('yourHashHere', filters).then((result) => {
9
//handle results here
10
console.log(result);
11
}).catch((err) => {
12
//handle error here
13
console.log(err);
14
});
Copied!

pinJSONToIPFS

Send JSON to Pinata for direct pinning to IPFS.
pinata.pinJSONToIPFS(body, options)
Params
  • body - Valid JSON you wish to pin to IPFS
  • options (optional): A JSON object that can contain the following keyvalues:
    • metadata (optional): A JSON object with optional metadata for the hash being pinned
    • pinataOptions (optional): A JSON object with additional options for the JSON being pinned
Response
1
{
2
IpfsHash: This is the IPFS multi-hash provided back for your content,
3
PinSize: This is how large (in bytes) the content you just pinned is,
4
Timestamp: This is the timestamp for your content pinning (represented in ISO 8601 format)
5
}
Copied!
Example Code
1
const body = {
2
message: 'Pinatas are awesome'
3
};
4
const options = {
5
pinataMetadata: {
6
name: MyCustomName,
7
keyvalues: {
8
customKey: 'customValue',
9
customKey2: 'customValue2'
10
}
11
},
12
pinataOptions: {
13
cidVersion: 0
14
}
15
};
16
pinata.pinJSONToIPFS(body, options).then((result) => {
17
//handle results here
18
console.log(result);
19
}).catch((err) => {
20
//handle error here
21
console.log(err);
22
});
Copied!

unpin

Have Pinata unpin content that you've pinned through the service.
pinata.unpin(hashToUnpin)
Params
  • hashToUnpin - the hash of the content you wish to unpin from Pinata
Response
If the operation is successful, you will simply receive "OK" as your result
Example Code
1
pinata.unpin(hashToUnpin).then((result) => {
2
//handle results here
3
console.log(result);
4
}).catch((err) => {
5
//handle error here
6
console.log(err);
7
});
Copied!

userPinPolicy

This allows the sender to change the pin policy their account.
Following a successful call of this endpoint, the new pin policy provided will be utilized for every new piece of content pinned to IPFS via Pinata.
To read more about pin policies, please check out the Regions and Replications Documentation.
pinata.userPinPolicy(newPinPolicy)
Params
  • newPinPolicy A JSON object with a new Pin Policy for the hash.
Response
If the operation is successful, you will receive back an "OK" REST 200 status.
Example Code
1
const newPinPolicy = {
2
regions: [
3
{
4
id: 'FRA1',
5
desiredReplicationCount: 2
6
},
7
{
8
id: 'NYC1',
9
desiredReplicationCount: 2
10
}
11
]
12
}
13
};
14
pinata.userPinPolicy(newPinPolicy).then((result) => {
15
//handle results here
16
console.log(result);
17
}).catch((err) => {
18
//handle error here
19
console.log(err);
20
});
Copied!

testAuthentication

Tests that you can authenticate with Pinata correctly
pinata.testAuthentication()
Params
None
Response
Example Code
1
pinata.testAuthentication().then((result) => {
2
//handle results here
3
console.log(result);
4
}).catch((err) => {
5
//handle error here
6
console.log(err);
7
});
Copied!

pinList

Retrieve pin records for your Pinata account
pinata.pinList(filters)
Params
  • filters (optional): An object that can consist of the following optional query parameters:
    • hashContains (optional): A string of alphanumeric characters that desires hashes must contain
    • pinStart (optional): The earliest date the content is allowed to have been pinned. Must be a valid ISO_8601 date.
    • pinEnd (optional): The earliest date the content is allowed to have been pinned. Must be a valid ISO_8601 date.
    • unpinStart (optional): The earlist date the content is allowed to have been unpinned. Must be a valid ISO_8601 date.
    • unpinEnd (optional): The latest date the content is allowed to have been unpinned. Must be a valid ISO_8601 date.
    • pinSizeMin (optional): The minimum byte size that pin record you're looking for can have
    • pinSizeMax (optional): The maximum byte size that pin record you're looking for can have
    • status (optional): Filter pins using one of the following options
      • 'all' (Records for both pinned and unpinned content will be returned)
      • 'pinned' (Only records for pinned content will be returned)
      • 'unpinned' (Only records for unpinned content will be returned)
    • pageLimit (optional): Limit the amount of results returned per page of results (default is 10, and max is 1000)
    • pageOffset (optional): Provide the record offset for records being returned. This is how you retrieve records on additional pages (default is 0)
    • metadata (optional): A JSON object that can be used to find records for content that had optional metadata included when it was added to Pinata. The metadata object is formatted as follows:
Metadata filter object formatting
1
{
2
name: 'exampleName',
3
keyvalues: {
4
testKeyValue: {
5
value: 'exampleFilterValue',
6
op: 'exampleFilterOperation'
7
},
8
testKeyValue2: {
9
value: 'exampleFilterValue2',
10
op: 'exampleFilterOperation2'
11
}
12
}
13
}
Copied!
Filter explanations:
  • name (optional): If provided, any records returned must have a name that contains the string provided for the 'name'.
  • keyvalues (optional): Each keyvalue provided in this object have both a value and op
    • value (required): This is the value which will be filtered on
    • op (required): This is the filter operation that will be applied to the value that was provided. Valid op values are:
      • 'gt' (greater than the value provided)
      • 'gte' (greater than or equal to the value provided)
      • 'lt' (less than the value provided)
      • 'lte' (less than or equal to the value provided)
      • 'ne' (not equal to the value provided)
      • 'eq' (equal to the value provided)
      • 'between' (between the two values provided) - NOTE - This also requires a secondValue be provided as seen in the example below
      • 'notBetween' (not between the two values provided) - NOTE - This also requires a secondValue be provided as seen in the example below
      • 'like' (like the value provided)
      • 'notLike' (not like the value provided)
      • 'iLike' (case insensitive version of like)
      • 'notILike' (case insensitive version of notLike)
      • 'regexp' (filter the value provided based on a provided regular expression)
      • 'iRegexp' (case insensitive version of regexp)
As an example, the following filter would only find records whose name contains the letters 'invoice', have the metadata key 'company' with a value of 'exampleCompany', and have a metadata key 'total' with values between 500 and 1000:
1
{
2
name: 'invoice',
3
keyvalues: {
4
company: {
5
value: 'exampleCompany,
6
op: 'eq'
7
},
8
total: {
9
value: 500,
10
secondValue: 1000,
11
op: 'between'
12
}
13
}
14
}
Copied!
Response
1
{
2
count: (this is the total number of pin records that exist for the query filters you passed in),
3
rows: [
4
{
5
id: (the id of your pin instance record),
6
ipfs_pin_hash: (the IPFS multi-hash for the content you pinned),
7
size: (this is how large (in bytes) the content pinned is),
8
user_id: (this is your user id for Pinata),
9
date_pinned: (This is the timestamp for when this content was pinned - represented in ISO 8601 format),
10
date_unpinned: (This is the timestamp for when this content was unpinned (if null, then you still have the content pinned on Pinata),
11
metadata: {
12
name: (this will be the name of the file originally upuloaded, or the custom name you set),
13
keyvalues: {
14
exampleCustomKey: "exampleCustomValue",
15
exampleCustomKey2: "exampleCustomValue2",
16
...
17
}
18
}
19
},
20
{
21
same record format as above
22
}
23
.
24
.
25
.
26
]
27
}
Copied!
Example Code
1
const metadataFilter = {
2
name: 'exampleName',
3
keyvalues: {
4
testKeyValue: {
5
value: 'exampleFilterValue',
6
op: 'exampleFilterOperation'
7
},
8
testKeyValue2: {
9
value: 'exampleFilterValue2',
10
op: 'exampleFilterOperation2'
11
}
12
}
13
};
14
15
const filters = {
16
status : 'pinned',
17
pageLimit: 10,
18
pageOffset: 0,
19
metadata: metadataFilter
20
};
21
pinata.pinList(filters).then((result) => {
22
//handle results here
23
console.log(result);
24
}).catch((err) => {
25
//handle error here
26
console.log(err);
27
});
Copied!

userPinnedDataTotal

Returns the total combined size (in bytes) of all content you currently have pinned on Pinata.
pinata.userPinnedDataTotal()
Params
None
Response
The response for this call will the total combined size of everything you currently have pinned on pinata. This value will be expressed in bytes
Example Code
1
pinata.userPinnedDataTotal().then((result) => {
2
//handle results here
3
console.log(result);
4
}).catch((err) => {
5
//handle error here
6
console.log(err);
7
});
Copied!

Host Node Multiaddresses

For endpoints that involve Pinata finding and pinning content that already resides on the IPFS network, you can help Pinata find your content faster by optionally providing us with the "multiaddresses" up to five host nodes that your content already resides on.
To find the multiaddresses of your nodes, simply run the following on your node's command line:
In the response, you'll want to focus on the "Addresses" array that's returned. Here you'll find the multiaddresses of your node. These multiaddresses are what other IPFS nodes use to connect to your node.
In the "Addresses" array, take note of the multiaddress that contains your external IP address. Not the local ipv4 "127.0.0.1" address or the local ipv6 "::1" address.
Here's an example of what a full external ipv4 multiaddress would look like (your IP address and node ID will differ):
/ip4/123.456.78.90/tcp/4001/ipfs/QmAbCdEfGhIjKlMnOpQrStUvWxYzAbCdEfGhIjKlMnOpQr
⚠️ Please make sure every node provided is online. Pinata will attempt to connect to all nodes before pinning the content, and if any these nodes are offline, your request will eventually fail.

Pin Policies

A pin policy tells Pinata how many times content should be replicated, and where that content should be replicated at.
To read more about pin policies, please check out the Regions and Replications Documentation.
Pin policies take the following form:
Example pin policy object
1
{
2
regions: [
3
{
4
id: 'FRA1',
5
desiredReplicationCount: 1
6
},
7
{
8
id: 'NYC1',
9
desiredReplicationCount: 2
10
}
11
]
12
}
Copied!
The ids of currently available public regions are:
• FRA1 - Frankfurt, Germany (max 2 replications)
• NYC1 - New York City, USA (max 2 replications)

Pinata Metadata

For endpoints that allow you to add content, Pinata lets you add optionally metadata for that content. This metadata can later be used for querying on what you've pinned with our userPinList endpoint. Providing metadata does not alter your content or how it is stored on IPFS in any way.
The metadata object can consist of the following values:
  • name (optional) - A custom string to use as the name for your content
  • keyvalues (optional) - An object containing up to 10 custom key / value pairs. The values can be:
    • strings
    • numbers (integers or decimals)
    • dates (provided in ISO_8601 format)
Example metadata object
1
{
2
name: "customName",
3
keyvalues: {
4
customKey: "customValue",
5
customKey2: "customValue2"
6
}
7
}
Copied!

Pinata Options

Some endpoints allow you to pass additional options for Pinata to take into account when adding content to IPFS.
The options object can consist of the following values:
  • cidVerson (optional) - The CID version IPFS will use when creating a hash for your content. Valid options are:
    • 0 - CIDv0
    • 1 - CIDv1
  • wrapWithDirectory (optional) - Tells IPFS to wrap your content in a directory to preserve the content's original name. See this blog post for more details on what this does. Valid options are:
    • true
    • false
  • customPinPolicy (optional) - a custom Pin Policy for the piece of content being pinned. Providing a custom pin policy as part of a request means that the content being pinned will be replicated differently from the user's default pin policy found under the Account page.
Example pinataOptions object
1
{
2
cidVersion: 1,
3
wrapWithDirectory: true,
4
customPinPolicy: {
5
regions: [
6
{
7
id: 'FRA1',
8
desiredReplicationCount: 2
9
},
10
{
11
id: 'NYC1',
12
desiredReplicationCount: 2
13
}
14
]
15
}
16
}
Copied!

Questions? Issues? Suggestions?

Feel free to file a github issue or email us at [email protected]
We'd love to hear from you!
Last modified 16d ago