• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PinataCloud/Pinata-SDK: Official SDK for the Pinata IPFS service

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

PinataCloud/Pinata-SDK

开源软件地址:

https://github.com/PinataCloud/Pinata-SDK

开源编程语言:

JavaScript 100.0%

开源软件介绍:

Pinata SDK

Official NodeJS SDK for Pinata

Overview

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

Installation

npm install --save @pinata/sdk

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.

const pinataSDK = require('@pinata/sdk');
const pinata = pinataSDK('yourPinataApiKey', 'yourPinataSecretApiKey');

Quickly test that you can connect to the API with the following call:

pinata.testAuthentication().then((result) => {
    //handle successful authentication here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

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:

keyvalues: {
    newKey: 'newValue', //this adds a keyvalue pair
    existingKey: 'newValue' //this modifies the value of an existing key if that key already exists
}
Removing keyvalues

To remove a keyvalue pair, simply provide null as the value for an existing key like so:

keyvalues: {
    existingKeyToRemove: null //this removes a keyvalue pair
}

Response

If the operation is successful, you will receive back an "OK" REST 200 status.

Example Code
const metadata = {
    name: 'new custom name',
    keyvalues: {
        newKey: 'newValue',
        existingKey: 'newValue',
        existingKeyToRemove: null
    }
};
pinata.hashMetadata('yourHashHere', metadata).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

pinByHash

Adds a hash to Pinata's pin queue to be pinned asynchronously. For the synchronous version of this operation see: pinHashToIPFS

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

Response

{
    id: This is Pinata's ID for the pin job,
    ipfsHash: This is the IPFS multi-hash provided to Pinata to pin,
    status: The current status of the pin job. If the request was successful the status should be 'searching'.
    name: The name of the pin (if provided initially)
}
Example Code
const options = {
    pinataMetadata: {
        name: MyCustomName,
        keyvalues: {
            customKey: 'customValue',
            customKey2: 'customValue2'
        }
    },
    pinataOptions: {
        hostNodes: [
            '/ip4/hostNode1ExternalIP/tcp/4001/ipfs/hostNode1PeerId',
            '/ip4/hostNode2ExternalIP/tcp/4001/ipfs/hostNode2PeerId'
        ]
    }
};
pinata.pinByHash('yourHashHere', options).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

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

{
    IpfsHash: This is the IPFS multi-hash provided back for your content,
    PinSize: This is how large (in bytes) the content you just pinned is,
    Timestamp: This is the timestamp for your content pinning (represented in ISO 8601 format)
}
Example Code
const fs = require('fs');
const readableStreamForFile = fs.createReadStream('./yourfile.png');
const options = {
    pinataMetadata: {
        name: MyCustomName,
        keyvalues: {
            customKey: 'customValue',
            customKey2: 'customValue2'
        }
    },
    pinataOptions: {
        cidVersion: 0
    }
};
pinata.pinFileToIPFS(readableStreamForFile, options).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

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

{
    IpfsHash: This is the IPFS multi-hash provided back for your content,
    PinSize: This is how large (in bytes) the content you just pinned is,
    Timestamp: This is the timestamp for your content pinning (represented in ISO 8601 format)
}
Example Code
const sourcePath = '/Users/me/builds/my-awesome-website/';
const options = {
    pinataMetadata: {
        name: 'My Awesome Website',
        keyvalues: {
            customKey: 'customValue',
            customKey2: 'customValue2'
        }
    },
    pinataOptions: {
        cidVersion: 0
    }
};
pinata.pinFromFS(sourcePath, options).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

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

{
    count: (this is the total number of pin job records that exist for the query filters you passed in),
    rows: [
        {
            id: (the id for the pin job record),
            ipfs_pin_hash: (the IPFS multi-hash for the content you pinned),
            date_queued: (The date this hash was initially queued to be pinned - represented in ISO 8601 format),
            name: (If you passed in a name for your hash, it will be listed here),
            status: (The current status for the pin job)
        },
        {
            same record format as above
        }
        .
        .
        .
    ]
}
Example Code
const filters = {
    sort: 'ASC',
    status: 'searching',
    ipfs_pin_hash: 'Qma6e8dovfLyiG2UUfdkSHNPAySzrWLX9qVXb44v1muqcp',
    limit: 10,
    offset: 0
};
pinata.pinJobs('yourHashHere', filters).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

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

{
    IpfsHash: This is the IPFS multi-hash provided back for your content,
    PinSize: This is how large (in bytes) the content you just pinned is,
    Timestamp: This is the timestamp for your content pinning (represented in ISO 8601 format)
}
Example Code
const body = {
    message: 'Pinatas are awesome'
};
const options = {
    pinataMetadata: {
        name: MyCustomName,
        keyvalues: {
            customKey: 'customValue',
            customKey2: 'customValue2'
        }
    },
    pinataOptions: {
        cidVersion: 0
    }
};
pinata.pinJSONToIPFS(body, options).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

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
pinata.unpin(hashToUnpin).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

上一篇:
jes/hardbin: Encrypted pastebin using IPFS发布时间:2022-06-22
下一篇:
textileio/powergate: Multitiered file storage API built on Filecoin and IPFS发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap