Skip to content

JavaScript HTTP

The JS HTTP Client was designed to work in the browser, using Node.js, or in Electron.

To use the JS HTTP Client, you'll need either a locally running Daemon or a locally running Desktop Tray instance containing a Textile account and available on localhost with default ports for both the API and Gateway.

Installation

The JS HTTP Client is published as an NPM Package. You are free to use the source code directly or use a package manager to install the official release.

NPM

npm install @textile/js-http-client

Yarn

yarn add @textile/js-http-client

TypeScript

We strongly recommend using TypeScript if you plan to use the JS Client in your project. The JS Client's endpoints are all typed and those types are available to projects that support them. Because Textile libraries are rapidly developing, this will make your upgrade process much easier and faster.

If you don't normally use TypeScript in your projects, never fear, it will be very familiar and only takes a few steps to setup.

Tip

We recommend TypeScript in 5 minutes as a good place to start if you are totally new to TypeScript.

Getting Started

Initialize the JS Client

The JS HTTP Client does not maintain any state for your app, it simply provides easy to use APIs so your app can interact with your user's Textile account. You import the library in any library simply.

// No initialization needed, only import
import textile from "@textile/js-http-client"

Below are some basic examples to get you started. If you are interested in a more thorough walk-through, check out the Tour of Textile's examples using the JS HTTP Client.

Get the account display name

const name = await textile.profile.name()

Subscribe to file updates

// The js-http-client returns a `ReadableStream` to be accessed by the caller
// See https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream for details
const stream = await textile.subscribe.stream("files")
const reader = stream.getReader()
const read = (result) => { // ReadableStreamReadResult<FeedItem>
  if (result.done) {
    return
  }
  try {
    console.log(result.value)  
  } catch (err) {
    reader.cancel(undefined)
    return
  }
  read(await reader.read())
}
read(await reader.read())

Create a thread

const thread = await textile.threads.add("Basic")

Add a file

const block = await textile.files.add("mmm, bytes...", "", "12D3KooWSYT6SUL9fx15pwjHSVUsuymnbixmRtPGySmFYtWE51Sc")

API Documentation

You can read the full API documentation for Textile's JavaScript HTTP Client (js-http-client) can be found at https://textileio.github.io/js-http-client.

Feel free to join the Textile Developer Slack and let us know what you are building. People are always excited to share and learn about new ideas.