@aioznetwork/aioz-pin-sdk
v1.0.1
Published
Experience seamless file management on our IPFS host with our SDK. Utilize API keys to effortlessly pin, unpin, retrieve, and securely access files through the gateway, ensuring efficient and secure file operations. Simplify decentralized file management
Readme
AIOZ Pin SDK
Description
Experience seamless file management on our IPFS host with our SDK. Utilize API keys to effortlessly pin, unpin, retrieve, and securely access files through the gateway, ensuring efficient and secure file operations. Simplify decentralized file management with ease and confidence.
Documentation
For comprehensive documentation and API references, visit:
Installation
npm install aioz-pin-sdkSetup
To start, simply require the AIOZ Pin SDK and set up an instance with your AIOZ Pin API Keys or your JWT key. Don't know what your keys are? Check out your Account Page.
For more information about the SDK and API, refer to the AIOZ Pin Documentation.
In the example below we provided with 2 ways to call the AIOZ Pin SDK.
// Use the api keys by providing the strings directly
import AiozPinClient from 'aioz-pin-sdk'
const client = new AiozPinClient('key', 'secret-key')Quickly test that you can connect to the API with the following call:
client
.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 AIOZ Pin SDK is easy. Simply call your desired function and handle the results of the promise.
Pinning
Nft
Data
pinByHash
The request body when pin a file by CID will look like this:
{
hash_to_pin: CID,
metadata: {
name: string,
keyvalues: {
key1: value1,
key2: value2
}
}
}Response
{
"data": {
"id": "string",
"file_record_id": "string",
"root_hash": "string",
"cid": "string",
"user_id": "string",
"date_pinned": "2023-01-01T11:11:11.111111Z",
"date_unpinned": "2023-11-11T11:11:11.111111Z",
"pinned": false,
"is_pin_by_hash": true,
"is_dir": false,
"metadata": {
"name": "string"
},
"status": "string"
},
"status": "success"
}Example Code
client
.pinByHash({
hashToPin: 'bafyb...2goq',
options: {
metadata: {
name: 'bafyb...2goq',
keyvalues: {
description1: 'this is description1',
description2: 'this is description2',
},
},
},
})
.then((res) => console.log({ res }))
.catch((err) => console.log({ err }))pinFilesToIPFS
This API allows you to pin a files to IPFS using the provided pinning API key and secret key.
Example metadata:
{"name": "sample name", "keyvalues":{"key1": "value1","key2": "value2"}}Response
{
"data": {
"id": "string",
"file_record_id": "string",
"root_hash": "string",
"cid": "string",
"user_id": "string",
"date_pinned": "2023-01-01T11:11:11.111111Z",
"date_unpinned": "2023-11-11T11:11:11.111111Z",
"pinned": false,
"is_pin_by_hash": false,
"sub_hash_status": "string",
"is_dir": false,
"metadata": {
"name": "string",
"type": "string"
},
"status": "string"
},
"status": "success"
}Example Code
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const filePath01 = path.join(__dirname, '../assets/file.txt')
const filePath02 = path.join(__dirname, '../assets/sample.json')
client
.pinFilesToIPFS({
filePaths: [filePath01, filePath02],
options: {
metadata: {
name: 'pin name',
keyvalues: {
description: 'This is a ipfs files',
},
},
},
})
.then((res) => console.log({ res }))
.catch((err) => console.log({ err }))pinFolderToIPFS
This API allows you to pin a folder to IPFS using the provided pinning API key and secret key.
Response
{
"data": {
"id": "string",
"file_record_id": "string",
"root_hash": "string",
"cid": "string",
"user_id": "string",
"date_pinned": "2023-01-01T11:11:11.111111Z",
"date_unpinned": "2023-11-11T11:11:11.111111Z",
"pinned": false,
"is_pin_by_hash": false,
"sub_hash_status": "string",
"is_dir": true,
"metadata": {
"name": "string",
"type": "string"
},
"status": "string"
},
"status": "success"
}Example Code
NOTE
The depth parameter in the pinFolderToIPFS function dictates the pinning behavior for files and subdirectories within the specified folder.
- When
depthis set toall, all files within the folder and its subdirectories will be pinned. - Setting
depthto0will directly pin files within the specified folder without considering its subdirectories. - If
depthis set to1, only one level of subdirectories will be pinned along with the files within the main folder.
Ensure to adjust the depth parameter based on your desired pinning depth.
const folderPath = 'c:/users/pin/folder'
client
.pinFolderToIPFS({
depth: 'all',
sourcePath: folderPath,
options: {
metadata: {
name: 'pin-name',
keyvalues: {
description: 'This is a folder',
},
},
},
})
.then((res) => console.log({ res }))
.catch((err) => console.log({ err }))unpin
Response
{
"data": {
"id": "string",
"file_record_id": "string",
"root_hash": "string",
"cid": "string",
"size": number,
"user_id": "string",
"date_pinned": "2023-01-01T11:11:11.111111Z",
"date_unpinned": "2023-11-11T11:11:11.111111Z",
"pinned": true,
"is_pin_by_hash": true,
"is_dir": false,
"metadata": {
"name": "string"
},
"status": "string"
},
"status": "success"
}Example Code
client
.unpin('file-id')
.then((result) => {
//handle results here
console.log(result)
})
.catch((err) => {
//handle error here
console.log(err)
})pinNft
The metadata JSON file will look like this:
{
"name": "My Awesome NFT",
"description": "This is an NFT that represents my creativity as a digital artist!",
"properties": [
{
"trait_type": "Color",
"value": "Red"
},
{
"trait_type": "Rarity",
"value": "Medium"
}
]
}Response
{
"data": {
"id": "string",
"asset_cid": "string",
"metadata_cid": "string",
"asset_pin_id": "string",
"metadata_pin_id": "string",
"size": number,
"user_id": "string",
"created_at": "2023-01-01T11:11:11.111111Z",
"updated_at": "2023-11-11T11:11:11.111111Z",
"pinned": true,
"metadata_asset": {
"name": "string",
"type": "string"
},
"status": "string"
},
"status": "success"
}Example Code
import * as fs from 'fs'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const imagePath = path.join(__dirname, './file.txt')
const metadataPath = path.join(__dirname, './sample.json')
const readableStreamForFile = fs.createReadStream(imagePath)
const readableStreamMetadataFile = fs.createReadStream(metadataPath)
// pin Nft by metadata object
client.pinNft({
fileStream: readableStreamForFile,
metadata: {
name: 'test',
description: 'test',
properties: [
{
trait_type: 'Color',
value: 'Red',
},
{
trait_type: 'Size',
value: 'M',
},
],
},
})
// pin Nft by file.json
client
.pinNftByStreamMetadata({
fileStream: readableStreamForFile,
metadataStream: readableStreamMetadataFile,
})
.then((res) => console.log({ res }))
.catch((err) => console.log({ err }))pinNftByHash
The metadata JSON file will look like this:
{
"name": "My Awesome NFT",
"description": "This is an NFT that represents my creativity as a digital artist!",
"properties": [
{
"trait_type": "Color",
"value": "Red"
},
{
"trait_type": "Rarity",
"value": "Medium"
}
]
}Response
{
"data": {
"id": "string",
"asset_cid": "string",
"metadata_cid": "string",
"asset_pin_id": "string",
"metadata_pin_id": "string",
"size": number,
"user_id": "string",
"created_at": "2023-01-01T11:11:11.111111Z",
"updated_at": "2023-11-11T11:11:11.111111Z",
"pinned": true,
"metadata_asset": {
"name": "string",
"type": "string"
},
"status": "string"
},
"status": "success"
}Example Code
import * as fs from 'fs'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const filePath = path.join(__dirname, '../assets/sample.json')
const readableStreamMetadataFile = fs.createReadStream(filePath)
client
.pinNftByHash({
hashToPin: 'bafkr...w7m',
metadata: {
name: 'My Awesome NFT',
description: 'This is an NFT that represents my creativity as a digital artist!',
properties: [
{
trait_type: 'Color',
value: 'Red',
},
{
trait_type: 'Rarity',
value: 'Medium',
},
],
},
})
.then((res) => console.log({ res }))
.catch((err) => console.log({ err }))
// pin nft by hash with metadata stream
client
.pinNftByHashAndMetadataStream({
hashToPin: 'bafk...f73u',
metadataStream: readableStreamMetadataFile,
})
.then((res) => console.log({ res }))
.catch((err) => console.log({ err }))unpinNft
Example Code
client
.unpinNft('nft-id')
.then((result) => {
//handle results here
console.log(result)
})
.catch((err) => {
//handle error here
console.log(err)
})testAuthentication
Response
{
"message": "Congratulations! You are communicating with the Web3 IPFS API!"
}Example Code
client
.testAuthentication()
.then((result) => {
//handle successful authentication here
console.log(result)
})
.catch((err) => {
//handle error here
console.log(err)
})getPinList
Response
{
"data": {
"totals": {
"files": number,
"size": number
},
"pins": [
{
"id": "string",
"file_record_id": "string",
"root_hash": "string",
"cid": "string",
"size": number,
"user_id": "string",
"date_pinned": "2023-01-01T11:11:11.111111Z",
"date_unpinned": "2023-11-11T11:11:11.111111Z",
"pinned": true,
"is_pin_by_hash": true,
"sub_hash_status": "string",
"is_dir": false,
"metadata": {
"name": "string"
},
"status": "string"
}
]
},
"status": "success"
}Example Code
client
.getPinList({
offset: 0,
limit: 10,
pinned: 'true',
sortBy: 'size',
sortOrder: 'DESC',
metadata: {
keyvalues: {
bbbn: '',
},
},
})
.then((result: PinListResponse) => {
console.log({ result: result.data })
})
.catch((error) => {
console.log(error)
})getPinByID
Response
{
"data": {
"id": "string",
"file_record_id": "string",
"root_hash": "string",
"cid": "string",
"size": number,
"user_id": "string",
"date_pinned": "2023-01-01T11:11:11.111111Z",
"date_unpinned": "2023-11-11T11:11:11.111111Z",
"pinned": true,
"is_pin_by_hash": true,
"sub_hash_status": "string",
"is_dir": false,
"metadata": {
"name": "string"
},
"status": "string"
},
"status": "success"
}Example Code
client
.getPinByID('pin id')
.then((result) => {
console.log({ result })
})
.catch((error) => {
console.log(error)
})getPinByCID
Response
{
"data": {
"id": "string",
"file_record_id": "string",
"root_hash": "string",
"cid": "string",
"size": number,
"user_id": "string",
"date_pinned": "2023-01-01T11:11:11.111111Z",
"date_unpinned": "2023-11-11T11:11:11.111111Z",
"pinned": true,
"is_pin_by_hash": true,
"sub_hash_status": "string",
"is_dir": false,
"metadata": {
"name": "string"
},
"status": "string"
},
"status": "success"
}Example Code
client
.getPinByCID('bafy...')
.then((result) => {
//handle results here
console.log(result)
})
.catch((err) => {
//handle error here
console.log(err)
})optimizeImage
This API allows you to optimize images stored on IPFS by fetching them through a gateway with various image transformation options. The optimized image is returned as binary data.
Reference: Image Optimization Documentation
Note: At least one option must be specified in the config parameter for the optimization feature to activate.
Response
Returns binary image data (ArrayBuffer when responseType: 'arraybuffer' is set in axiosConfig).
Example Code
Basic usage:
client
.optimizeImage({
cid: 'YOUR CID',
gatewayProviderUrl: 'GATEWAY URL',
config: {
imgWidth: 800,
imgQuality: 75,
imgFit: 'cover',
imgGravity: '0.5x0.5',
},
axiosConfig: {
responseType: 'arraybuffer',
},
})
.then((result) => {
// result is an ArrayBuffer containing the optimized image
const buffer = Buffer.from(result)
console.log(`Image size: ${buffer.length} bytes`)
})
.catch((error) => {
console.log(error)
})Saving optimized image to file:
import { writeFileSync } from 'fs'
import { join } from 'path'
client
.optimizeImage({
cid: 'YOUR CID',
gatewayProviderUrl: 'GATEWAY URL',
config: {
imgWidth: 800,
imgQuality: 100,
imgFit: 'cover',
},
axiosConfig: {
responseType: 'arraybuffer',
},
})
.then((result) => {
// Convert ArrayBuffer to Buffer for file writing
const buffer = Buffer.from(result)
// Save the file
const outputPath = join(process.cwd(), 'optimized-image.jpg')
writeFileSync(outputPath, buffer)
console.log(`Image saved successfully to: ${outputPath}`)
console.log(`File size: ${buffer.length} bytes`)
})
.catch((error) => {
console.log(error)
})Advanced configuration example:
client
.optimizeImage({
cid: 'YOUR CID',
gatewayProviderUrl: 'GATEWAY URL',
config: {
imgWidth: 1920,
imgHeight: 1080,
imgFit: 'crop',
imgGravity: '0.5x0.5',
imgQuality: 90,
imgDpr: 2,
imgFormat: 'webp',
imgAnim: false,
imgSharpen: 5,
imgMetadata: 'copyright',
imgOnError: 'redirect',
},
axiosConfig: {
responseType: 'arraybuffer',
},
})
.then((result) => {
// Handle the optimized image
console.log('Image optimized successfully')
})
.catch((error) => {
console.log(error)
})