Upload File

The uploadFile function in the Tatum SDK stores a file passed as a buffer on IPFS and returns its CID (ipfsHash), a unique reference to the data.

The "uploadFile" function is specifically designed to facilitate the process of uploading data to the IPFS network. By supplying the file in a buffer format, this function allows users to store their data on the decentralised IPFS system and receive a unique CID (Content Identifier) in return, which acts as a reference to the uploaded data. To write log records to the blockchain instead, see Blockchain storage: IPFS files and onchain logs.

How to upload a file to IPFS using TatumSDK

Use the TatumSDK (@tatumio/tatum) to upload your file to IPFS.

// yarn add @tatumio/tatum
import { TatumSDK, Network, Polygon } from '@tatumio/tatum'
import { readFileSync } from 'fs'

const tatumClient = await TatumSDK.init<Polygon>({
  network: Network.POLYGON,
  apiKey: '{YOUR_API_KEY}',
})

const buffer = readFileSync('/path/to/file/file.jpg')

const result = await tatumClient.ipfs.uploadFile({ file: buffer })
console.log(JSON.stringify(result))

await tatumClient.destroy()
// Install with: npm install @tatumio/tatum
const { TatumSDK, Network } = require('@tatumio/tatum')
const { readFileSync } = require('fs')

async function main() {
  const tatumClient = await TatumSDK.init({
    network: Network.POLYGON,
    apiKey: '{YOUR_API_KEY}',
  })

  const buffer = readFileSync('/path/to/file/file.jpg')

  const result = await tatumClient.ipfs.uploadFile({ file: buffer })
  console.log(JSON.stringify(result))

  await tatumClient.destroy()
}

main()

Expected Response

{
  "data": {
    "ipfsHash": "bafybeighfkqyizr3ncoraqfyi6twf2tnxjxhxewwaqvdom57ijiudh5w7y"
  },
  "status": "SUCCESS"
}

Request interface

interface UploadFileDetails {
  /**
   * The file is to be uploaded in binary format.
   */
  file: Buffer
}

Response interface

interface ResponseDto<T> {
  /**
   * Actual payload of the response.
   */
  data: T
  /**
   * Status of the response.
   */
  status: Status
  /**
   * In case of ERROR status, this field contains the error message and detailed description.
   */
  error?: ErrorWithMessage
}

interface IPFSUploadResponse {
  /**
   * The unique CID (Content Identifier) associated with the uploaded file.
   */
  ipfsHash: string
}

Did this page help you?