You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/basics/js/js-ipfs.md
+21-32Lines changed: 21 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,23 @@
1
1
---
2
2
title: IPFS in JavaScript
3
-
description: "A simple walkthrough of how to perform basic IPFS operations using the JS implementation."
3
+
description: "A simple walkthrough of how to perform basic IPFS operations using the JavaScript implementation."
4
4
---
5
5
6
-
# JS-IPFS Basics
6
+
# IPFS in JavaScript
7
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).
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). You will learn how install and spawn a node using the available libraries, as well as add, retrieve, read, and remove files. If you are unsure about the meaning of some terms, check out the [glossary](../concepts/glossary.md).
12
9
13
10
::: tip Environment
14
11
15
12
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
13
17
14
:::
18
15
19
-
There are two main JavaScript libraries for working with IPFS, learn about each library in the [reference section](../reference/js/api).
20
-
21
16
## Install JS-IPFS
22
17
23
18
:::: tabs
24
19
25
-
::: tab ipfs-cli
20
+
::: tab ipfs-cli id="install-ipfs-cli"
26
21
27
22
### JS-IPFS module
28
23
@@ -38,7 +33,7 @@ To build from source, clone the [source packages](https://github.com/ipfs/js-ipf
38
33
39
34
:::
40
35
41
-
::: tab ipfs-core
36
+
::: tab ipfs-core id="install-ipfs-core"
42
37
43
38
### IPFS core API
44
39
@@ -60,7 +55,7 @@ To build from source, clone the [source packages](https://github.com/ipfs/js-ipf
60
55
61
56
:::: tabs
62
57
63
-
::: tab ipfs-cli
58
+
::: tab ipfs-cli id="spawn-ipfs-cli"
64
59
65
60
To spawn a node using the CLI, simply start the daemon:
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.
105
100
@@ -143,7 +138,7 @@ To create an IPFS node, add:
143
138
main();
144
139
```
145
140
146
-
This imports IPFS as a dependency and uses the `create()` function to create a node instance.
141
+
This imports IPFS as a dependency and uses the `create()` function to create a node instance.
147
142
148
143
To spawn the node, run the application:
149
144
@@ -169,7 +164,7 @@ The JS-IPFS implementation is split into several Node.js modules. The following
If you have not already installed the client library, add the `ipfs-http-client` module to your project:
175
170
@@ -185,13 +180,13 @@ Populate your `index.js` file with the following to create an instance of the HT
185
180
const client = create() // the default API address http://localhost:5001
186
181
```
187
182
188
-
This imports the client library and uses the `create()` function to connect to an IPFS API server.
183
+
This imports the client library and uses the `create()` function to connect to an IPFS API server.
189
184
190
185
To connect to the API, run the application:
191
186
192
187
:::
193
188
194
-
::: tab ipfs-client
189
+
::: tab ipfs-client id="connect-ipfs-client"
195
190
196
191
If you have not already installed the client library, add the `ipfs-http-client` module to your project:
197
192
@@ -222,15 +217,13 @@ Now you can start to add files using JS-IPFS to the IPFS network.
222
217
223
218
::: warning Section changes coming soon
224
219
225
-
As the JS-IPFS implementation goes through changes, the steps to add a
226
-
file are likely to change. Please reference the
227
-
[source packages](https://github.com/ipfs/js-ipfs) for the latest updates.
220
+
As the JS-IPFS implementation goes through changes, the steps to add a file are likely to change. Please reference the [source packages](https://github.com/ipfs/js-ipfs) for the latest updates.
228
221
229
222
:::
230
223
231
224
:::: tabs
232
225
233
-
::: tab ipfs-cli
226
+
::: tab ipfs-cli id="add-ipfs-cli"
234
227
235
228
In a new session, navigate to the directory you wish to add a file from. You can also specify the file path when using the cli to add a file.
236
229
@@ -256,14 +249,11 @@ To view the file contents, navigate to the [webui](http://127.0.0.1:5002/webui)
256
249
257
250
:::
258
251
259
-
::: tab ipfs-core
252
+
::: tab ipfs-core id="add-ipfs-core"
260
253
261
-
To add a file using `ipfs-core`, you can create a test `.txt` file in
262
-
your project directory or point to a local file on your machine that you would
263
-
like to upload to IPFS.
254
+
To add a file using `ipfs-core`, you can create a test `.txt` file in your project directory or point to a local file on your machine that you would like to upload to IPFS.
264
255
265
-
Then, using `node.add`, add an `await` operator that includes a `path`
266
-
and `content` field and an output message in the project's `index.js` file:
256
+
Then, using `node.add`, add an `await` operator that includes a `path` and `content` field and an output message in the project's `index.js` file:
267
257
268
258
```js{6-9,11}
269
259
import * as IPFS from 'ipfs-core';
@@ -288,11 +278,10 @@ You should obtain an output similar to:
The file has been added to the IPFS network and has given the file a CID.
292
-
You can share this CID with anyone, and they can use it on their IPFS node to obtain
293
-
the content you uploaded.
281
+
The file has been added to the IPFS network and has given the file a CID. You can share this CID with anyone, and they can use it on their IPFS node to obtain the content you uploaded.
294
282
295
283
If you take the CID and load it on the HTTP gateway, you will see the content:
284
+
296
285
> e.g. https://ipfs.io/ipfs/QmYt9ypyGsR1BKdaCGPdwdBgAiuXK5AYN2bGSNZov7YXuk
@@ -305,7 +294,7 @@ If you take the CID and load it on the HTTP gateway, you will see the content:
305
294
306
295
::::tabs
307
296
308
-
::: tab ipfs-cli
297
+
::: tab ipfs-cli id="retrieve-ipfs-cli"
309
298
310
299
Navigate to the directory where you wish to save the folder. IPFS will save the folder to whichever directory you are in. Here, we're going to save the file in the ~/Desktop directory:
311
300
@@ -329,7 +318,7 @@ You will notice a new file in your project directory that is labelled as the CID
329
318
330
319
:::
331
320
332
-
::: tab ipfs-core
321
+
::: tab ipfs-core id="retrieve-ipfs-core"
333
322
334
323
Continuing with the same Node.js application, retrieving a file from IPFS can be done by using
335
324
a `cat` call. We will use the CID from the previous section when we added the `test.txt` file by passing `fileAdded.cid` as an argument to `node.cat`:
@@ -397,7 +386,7 @@ Removing the content pin will remove a file from IPFS. In this section, we will
397
386
398
387
:::: tabs
399
388
400
-
::: tab ipfs-cli
389
+
::: tab ipfs-cli id="remove-ipfs-cli"
401
390
402
391
If you would like to remove a different piece of content, you can run `jsipfs pin ls` to view a list of pinned content on the local IPFS node:
0 commit comments