Filebase NPM Package
Learn how to use the Filebase NPM package for pinning files to IPFS.
What is the Filebase NPM package?
The Filebase NPM package is a client library for the https://filebase.com/ service. It provides a convenient interface for working with the Raw HTTP API from a web browser or Node.js and comes bundled with TS for out-of-the box type inference and better IntelliSense.
Read below to learn how to use the Filebase NPM package for pinning files to IPFS.
Installing the Filebase npm package
Install the package using npm:
npm i @filebase/client
Or install the package using yarn:
yarn add @filebase/client
Pinning New Files
import { FilebaseClient, File } from '@filebase/client'
const filebaseClient = new FilebaseClient({ token: 'API_TOKEN' })
const metadata = await filebaseClient.store({
name: 'Example',
description: 'An example image.',
image: new File(
[
/* data */
],
'example.jpg',
{ type: 'image/jpg' }
),
})
console.log(metadata.url)
// ipfs://bafyreib4pff766vhpbxbhjbqqnsh5emeznvujayjj4z2iu533cprgbz23m/metadata.json
Replace the following values to reflect your configuration:
API Token: Your Filebase API Token. You can learn how to obtain this value here.
Name: Your file name.
Description: Your file description
example.jpg: The name of the file to be pinned.
Note: The client uses ESM modules. If running from Node.js, either name your script index.mjs
or name it index.js
and use npm init
to create a new package.json
file in your project directory, adding "type": "module",
to it.
Alternatively, you can pass text content directly in the script to be uploaded as a new file:
import { FilebaseClient } from '@filebase/client'
const filebaseClient = new FilebaseClient({ token: 'API_TOKEN' })
const content = new Blob(['hello world'])
const cid = await filebaseClient.storeBlob(content)
console.log(cid);
// Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD
Running the Script
node index.js
The output will resemble the following:

Last updated
Was this helpful?