# Filebase NPM Package

## 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.

{% hint style="success" %}

### Prerequisites:

* [x] Download and install [NodeJS](https://nodejs.org/en/download/) version 14.20.0 or newer.
* [x] [Sign up](https://filebase.com/signup) for a free Filebase account.
* [x] Have your Filebase Access and Secret Keys. Learn how to view your access keys [here](https://docs.filebase.com/getting-started-guides/getting-started-guide#working-with-access-keys).
* [x] Create a Filebase IPFS Bucket. Learn how to create a bucket [here](https://docs.filebase.com/getting-started-guides/getting-started-guide#creating-and-working-with-buckets).
  {% endhint %}

## 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

```jsx
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](https://docs.filebase.com/api-documentation/ipfs-pinning-service-api).
* **Name**: Your file name.
* **Description**: Your file description
* **example.jpg**: The name of the file to be pinned.&#x20;

{% hint style="warning" %}
**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.
{% endhint %}

Alternatively, you can pass text content directly in the script to be uploaded as a new file:

```jsx
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:

<figure><img src="https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FUfZLtZLKKrQLN3eAQWle%2Fimage.png?alt=media&#x26;token=3aaef16c-b480-4774-a783-a56ac6824033" alt=""><figcaption></figcaption></figure>
