|
| 1 | +--- |
| 2 | +title: IPFS in JavaScript |
| 3 | +description: "A simple walkthrough of how to perform basic IPFS operations using the JS implementation." |
| 4 | +--- |
| 5 | + |
| 6 | +# JS-IPFS Basics |
| 7 | + |
| 8 | +This guide aims to walk you through the basics of using IPFS with JavaScript. JS-IPFS is one of multiple [IPFS implementations](../ipfs-implementations.md). |
| 9 | + |
| 10 | +You will learn how install and spawn a node using the available libraries, as well as add, retrieve, |
| 11 | +read, and remove files. If you are unsure about the meaning of some terms, check out the [glossary](../concepts/glossary.md). |
| 12 | + |
| 13 | +::: tip Environment |
| 14 | + |
| 15 | +All instructions and examples shown here were performed and tested on an M1 Mac. However, the IPFS commands are the same on Linux, macOS, and Windows. You will need to know how to navigate your computer's directories from within the CLI. If you're unsure how to use the CLI, we recommend learning how before continuing with this guide. |
| 16 | + |
| 17 | +::: |
| 18 | + |
| 19 | +There are two main JavaScript libraries for working with IPFS, learn about each library in the [reference section](../reference/js/api). |
| 20 | + |
| 21 | +## Install JS-IPFS |
| 22 | + |
| 23 | +:::: tabs |
| 24 | + |
| 25 | +::: tab ipfs-cli |
| 26 | + |
| 27 | +### JS-IPFS module |
| 28 | + |
| 29 | +To use the CLI on your machine, globally install the `ipfs` Node.js package: |
| 30 | + |
| 31 | +```bash |
| 32 | +npm i --location=global ipfs |
| 33 | +``` |
| 34 | + |
| 35 | +### Build from source |
| 36 | + |
| 37 | +To build from source, clone the [source packages](https://github.com/ipfs/js-ipfs) and follow the build instructions. |
| 38 | + |
| 39 | +::: |
| 40 | + |
| 41 | +::: tab ipfs-core |
| 42 | + |
| 43 | +### IPFS core API |
| 44 | + |
| 45 | +To use the IPFS core API, install the `ipfs-core` Node.js package: |
| 46 | + |
| 47 | +```bash |
| 48 | +npm i ipfs-core |
| 49 | +``` |
| 50 | + |
| 51 | +### Build from source |
| 52 | + |
| 53 | +To build from source, clone the [source packages](https://github.com/ipfs/js-ipfs) and follow the build instructions. |
| 54 | + |
| 55 | +::: |
| 56 | + |
| 57 | +::: tab ipfs-http-client |
| 58 | + |
| 59 | +### HTTP client module |
| 60 | + |
| 61 | +To use the client on your machine, install the `ipfs-http-client` Node.js package: |
| 62 | + |
| 63 | +```bash |
| 64 | +npm i ipfs-http-client |
| 65 | +``` |
| 66 | + |
| 67 | +### Build from source |
| 68 | + |
| 69 | +To build from source, clone the [source package](https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs-http-client) and follow the build instructions. |
| 70 | + |
| 71 | +::: |
| 72 | + |
| 73 | +::: tab ipfs-client |
| 74 | + |
| 75 | +### HTTP client module |
| 76 | + |
| 77 | +To use the client on your machine, install the `ipfs-client` Node.js package: |
| 78 | + |
| 79 | +```bash |
| 80 | +npm i ipfs-client |
| 81 | +``` |
| 82 | + |
| 83 | +### Build from source |
| 84 | + |
| 85 | +To build from source, clone the [source package](https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs-http-client) and follow the build instructions. |
| 86 | + |
| 87 | +::: |
| 88 | + |
| 89 | +:::: |
| 90 | + |
| 91 | +## Spawn a node |
| 92 | + |
| 93 | +:::: tabs |
| 94 | + |
| 95 | +::: tab ipfs-cli |
| 96 | + |
| 97 | +To spawn a node using the CLI, simply start the daemon: |
| 98 | + |
| 99 | +```bash |
| 100 | +jsipfs daemon |
| 101 | +``` |
| 102 | + |
| 103 | +You should see an output similar to: |
| 104 | + |
| 105 | +```shell |
| 106 | +Initializing IPFS daemon... |
| 107 | +System version: arm64/darwin |
| 108 | +Node.js version: 16.16.0 |
| 109 | +Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/12D3KooWMZr34r6FArFH36QxyT25BM4HL4u2WF7jQzwNdg91awB6 |
| 110 | +Swarm listening on /ip4/10.0.0.25/tcp/4002/p2p/12D3KooWMZr34r6FArFH36QxyT25BM4HL4u2WF7jQzwNdg91awB6 |
| 111 | +Swarm listening on /ip4/10.2.0.2/tcp/4002/p2p/12D3KooWMZr34r6FArFH36QxyT25BM4HL4u2WF7jQzwNdg91awB6 |
| 112 | +Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/12D3KooWMZr34r6FArFH36QxyT25BM4HL4u2WF7jQzwNdg91awB6 |
| 113 | +js-ipfs version: 0.15.4 |
| 114 | +HTTP API listening on /ip4/127.0.0.1/tcp/5002/http |
| 115 | +gRPC listening on /ip4/127.0.0.1/tcp/5003/ws |
| 116 | +Gateway (read only) listening on /ip4/127.0.0.1/tcp/9090/http |
| 117 | +Web UI available at http://127.0.0.1:5002/webui |
| 118 | +Daemon is ready |
| 119 | +``` |
| 120 | + |
| 121 | +You should be able to point to the [webpage](http://127.0.0.1:5002/webui): |
| 122 | + |
| 123 | +<img src="../../images/jsipfs-webui.png" width="1000"> |
| 124 | + |
| 125 | +If you are unable to connect to the API, ensure [cross-origin (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) requests are configured: |
| 126 | + |
| 127 | +```bash |
| 128 | +jsipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://127.0.0.1:5002", "http://localhost:3000", "http://127.0.0.1:5001", "https://webui.ipfs.io"]' |
| 129 | +jsipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST"]' |
| 130 | +``` |
| 131 | + |
| 132 | +::: |
| 133 | + |
| 134 | +::: tab ipfs-core |
| 135 | + |
| 136 | +Create a simple Node.js application to host the logic that will allow you to use the RPC API. You'll also use this Node.js application later on to add and remove files. |
| 137 | + |
| 138 | +Start by initiating a new project: |
| 139 | + |
| 140 | +```bash |
| 141 | +npm init -y |
| 142 | +``` |
| 143 | + |
| 144 | +If you have not already installed `ipfs-core`, add the `ipfs-core` module to your project: |
| 145 | + |
| 146 | +```bash |
| 147 | +npm i ipfs-core |
| 148 | +``` |
| 149 | + |
| 150 | +Create an `index.js` file for the application logic: |
| 151 | + |
| 152 | +```bash |
| 153 | +touch index.js |
| 154 | +``` |
| 155 | + |
| 156 | +Now, populate the `index.js` file by initiating an `async` function: |
| 157 | + |
| 158 | +```js |
| 159 | +const main = async () => { |
| 160 | + // "await" logic to spawn a node |
| 161 | +} |
| 162 | + |
| 163 | +main() |
| 164 | +``` |
| 165 | + |
| 166 | +To create an IPFS node, add: |
| 167 | + |
| 168 | +```js{1,4} |
| 169 | +import * as IPFS from 'ipfs-core'; |
| 170 | +
|
| 171 | +async function main() { |
| 172 | + const node = await IPFS.create(); |
| 173 | +} |
| 174 | +
|
| 175 | +main(); |
| 176 | +``` |
| 177 | + |
| 178 | +This imports IPFS as a dependency and uses the `create()` function to create a node instance. |
| 179 | + |
| 180 | +To spawn the node, run the application: |
| 181 | + |
| 182 | +```bash |
| 183 | +node index.js |
| 184 | +``` |
| 185 | + |
| 186 | +You should see an output similar to: |
| 187 | + |
| 188 | +```shell |
| 189 | +Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/12D3KooWMZr34r6FArFH36QxyT25BM4HL4u2WF7jQzwNdg91awB6 |
| 190 | +Swarm listening on /ip4/10.0.0.25/tcp/4002/p2p/12D3KooWMZr34r6FArFH36QxyT25BM4HL4u2WF7jQzwNdg91awB6 |
| 191 | +Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/12D3KooWMZr34r6FArFH36QxyT25BM4HL4u2WF7jQzwNdg91awB6 |
| 192 | +``` |
| 193 | + |
| 194 | +::: |
| 195 | + |
| 196 | +:::: |
| 197 | + |
| 198 | +## Connect to IPFS |
| 199 | + |
| 200 | +:::: tabs |
| 201 | + |
| 202 | +::: tab ipfs-http-client |
| 203 | + |
| 204 | +If you have not already installed the client library, add the `ipfs-http-client` module to your project: |
| 205 | + |
| 206 | +```bash |
| 207 | +npm i ipfs-http-client |
| 208 | +``` |
| 209 | + |
| 210 | +Populate your `index.js` file with the following to create an instance of the HTTP API client: |
| 211 | + |
| 212 | +```js{1,3} |
| 213 | +import { create } from 'ipfs-http-client' |
| 214 | +
|
| 215 | +const client = create() // the default API address http://localhost:5001 |
| 216 | +``` |
| 217 | + |
| 218 | +This imports the client library and uses the `create()` function to connect to an IPFS API server. |
| 219 | + |
| 220 | +To connect to the API, run the application: |
| 221 | + |
| 222 | +::: |
| 223 | + |
| 224 | +::: tab ipfs-client |
| 225 | + |
| 226 | +If you have not already installed the client library, add the `ipfs-http-client` module to your project: |
| 227 | + |
| 228 | +Populate your `index.js` file with the following to create an instance of the HTTP API client: |
| 229 | + |
| 230 | +```js{1,3-5} |
| 231 | +import { create } from 'ipfs-client' |
| 232 | +
|
| 233 | +const client = create({ |
| 234 | + grpc: '/ipv4/127.0.0.1/tcp/5003/ws', |
| 235 | + http: '/ipv4/127.0.0.1/tcp/5002/http' |
| 236 | +}) |
| 237 | +
|
| 238 | +const id = await client.id() |
| 239 | +``` |
| 240 | + |
| 241 | +This imports the client library and uses the `create()` function to define the server endpoints. |
| 242 | + |
| 243 | +To connect to the endpoints, run the application: |
| 244 | + |
| 245 | +::: |
| 246 | + |
| 247 | +:::: |
| 248 | + |
| 249 | +## Add, retrieve & remove files |
| 250 | + |
| 251 | +::: warning Section coming soon |
| 252 | + |
| 253 | +As the JS-IPFS implementation goes through changes, adding and removing a file are subject to change. For the time being, please reference the [source packages](https://github.com/ipfs/js-ipfs). |
| 254 | + |
| 255 | +::: |
0 commit comments