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.

Prerequisites:

  • Download and install NodeJS version 14.20.0 or newer.
  • Sign up for a free Filebase account.
  • Have your Filebase Access and Secret Keys. Learn how to view your access keys here.
  • Create a Filebase IPFS Bucket. Learn how to create a bucket here.

1. Install the Filebase npm package:

Install the package using npm:
npm i @filebase/client
Or install the package using yarn:
yarn add @filebase/client

Create a new file called index.js. Insert the following content:

const fsPath = require("path");
const { FilebaseClient } = require('@filebase/client')
const { filesFromPath } = require('files-from-path')
async function upload (api, options) {
console.log(`Parsing options...`);
const { path, pinName, verbose } = options;
let source = path;
if (!fsPath.isAbsolute(source)) {
const dir = process.cwd().toString();
source = fsPath.join(dir, source);
}
console.log(`Adding files...`);
const files = [];
for await (const file of filesFromPath(source, { pathPrefix: source })) {
files.push(file)
if (verbose) {
console.log(`Added File: ${JSON.stringify(file)}`);
}
}
console.log(`Storing files...`);
let tokenString = `${api.key}:${api.secret}:${api.bucket}`;
let cid = await FilebaseClient.storeDirectory(
{ endpoint: 'https://s3.filebase.com', token: Buffer.from(tokenString).toString('base64') },
files,
pinName
);
console.log(`Stored files...`)
console.log(`CID: ${cid}`);
console.log(`Done`)
return {
cid: cid,
ipfs: cid,
};
}
(async function () {
try {
await upload({
key: "FILEBASE_KEY",
secret: "FILEBASE_SECRET",
bucket: "FILEBASE_BUCKET",
}, {
path: "testFiles",
pinName: "myFirstIpfs3Pin"
});
} catch (err) {
console.error(err.message);
}
})();
Replace the following values to reflect your configuration:
  • Key: Filebase Access Key
  • Secret: Filebase Secret Access Key
  • Bucket: Filebase IPFS Bucket Name
  • Path: Path to files to pin to IPFS
  • PinName: The name for the pinned file
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.

3. Run this file with the command:

node index.js
The output will resemble the following:
If you have any questions, please join our Discord server, or send us an email at [email protected]
Copy link
On this page
What is the Filebase NPM package?
1. Install the Filebase npm package:
Create a new file called index.js. Insert the following content:
3. Run this file with the command: