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
`bee-js` simplifies development on Swarm by abstracting away many of finer details and quirks of the Bee API so that you can focus on building your dream DAPP with minimal hassle. It's the easiest way to get started developing on Swarm.
After that you need to import the Bee class and create a bee instance connecting to your Bee node(here we assume it runs on localhost on default port).
48
+
After that you need to import the `Bee` class and initialize an instance of it using our Bee node's API endpoint (here we assume it runs on localhost on the default port).
50
49
51
-
Be aware, if you will pass invalid URL the constructor will throw an exception!
50
+
:::info Run your own Bee node
51
+
You can find out more about setting up a Bee node and getting your node's API endpoint in the [Bee docs](https://docs.ethswarm.org/docs/installation/quick-start)
52
+
:::
52
53
53
54
```js
54
55
import { Bee } from"@ethersphere/bee-js"
@@ -58,10 +59,6 @@ const bee = new Bee('http://localhost:1633')
58
59
59
60
That’s it! now you can use the `bee` object.
60
61
61
-
:::info Run your own Bee node
62
-
You can find out more about running Bee node in the [Bee docs](https://docs.ethswarm.org/docs/installation/quick-start)
63
-
:::
64
-
65
62
:::tip Using `<script>` import
66
63
67
64
If you include `bee-js` using the `unpkg.com` script link then all the exported components will be available to you
@@ -76,33 +73,47 @@ under global namespace `BeeJs`:
76
73
:::
77
74
78
75
79
-
## Quickstart With*create-swarm-app*
76
+
## Quickstart with*create-swarm-app*
80
77
81
-
The `create-swarm-app` tool makes it easy to get started developing on Swarm. With a single command, it generates the basic scaffolding for a `bee-js`project in your chosen development environment (CommonJS, ESM, TypeScript, or Vite + TypeScript).
78
+
The `create-swarm-app` tool generates ready‑to‑run skeletons for `bee-js`projects in your chosen setup (CommonJS, ESM, TypeScript, or Vite + TypeScript) with a single command.
82
79
83
-
:::note WSL WARNING
80
+
:::warning WSL WARNING
84
81
The `create-swarm-app` tool is compatible with Windows, macOS, and Linux. However, using it in combination with [WSL](https://learn.microsoft.com/en-us/windows/wsl/) is discouraged due to potential compatibility issues that may require additional troubleshooting.
85
82
86
83
That said, the `bee-js` library itself works seamlessly within WSL. If you prefer to develop your project using WSL, you can use `create-swarm-app` to generate the project files on the Windows side, then move them into your WSL environment for development.
87
84
:::
88
85
89
-
You can use [`create-swarm-app`](https://www.npmjs.com/package/create-swarm-app) to quickly set up scaffolding for a `bee-js` project with the following command:
86
+
### 1) Choose a template and scaffold your app
90
87
91
88
```bash
92
-
npm init swarm-app@latest app-nameapp-type
89
+
npm init swarm-app@latest <app-name><app-type>
93
90
```
94
91
95
-
Replace "app-name" with your app's name, and "app-type" with the type of app you want. Supported types are `node`, `node-esm`, `node-ts` and `vite-tsx`.
92
+
Replace `<app-name>` with your project name, and `<app-type>` with one of:
93
+
94
+
*`node` – Node.js (CommonJS)
95
+
*`node-esm` – Node.js (ES modules)
96
+
*`node-ts` – Node.js (TypeScript)
97
+
*`vite-tsx` – Vite + React + TypeScript (front‑end)
These outputs give you a minimal script that connects to a Bee node, ensures you have a usable postage batch, then uploads and downloads data via `bee-js`.
102
+
103
+
Example (TypeScript):
98
104
99
105
```bash
100
106
npm init swarm-app@latest my-dapp node-ts
107
+
108
+
# then
109
+
cd my-dapp
110
+
npm install
111
+
npm start
101
112
```
102
113
103
-
Your project structure will look like:
114
+
Project structure:
104
115
105
-
```bash
116
+
```text
106
117
.
107
118
├── package.json
108
119
├── src
@@ -111,13 +122,70 @@ Your project structure will look like:
111
122
└── tsconfig.json
112
123
```
113
124
114
-
or using Vite and TypeScript:
125
+
`src/config.ts` — Bee API endpoint (adjust if your node is not on the default):
// amount and depth are examples; tune for your needs
160
+
returnbee.createPostageBatch('500000000', 20)
161
+
}
162
+
}
163
+
```
164
+
165
+
> The CommonJS (`node`) and ESM (`node-esm`) templates include the same basic logic adjusted for their respective module systems.
166
+
167
+
### 3) Front‑end template (`vite-tsx`)
168
+
169
+
This option generates a simple React + Vite app that talks directly to Bee from the browser.
170
+
171
+
In contrast with the previous example, the `vite-tsx` option for `create-swarm-app` will output the basic scaffolding for a Swarm integrated static site which can be uploaded to Swarm directly - no servers needed!
115
172
116
173
```bash
117
174
npm init swarm-app@latest my-dapp vite-tsx
118
175
```
119
176
120
-
Your project structure will look like:
177
+
```bash
178
+
> npx
179
+
> create-swarm-app my-dapp vite-tsx
180
+
181
+
Project created
182
+
183
+
cd my-dapp
184
+
npm install
185
+
npm start
186
+
```
187
+
188
+
The output files will have this structure:
121
189
122
190
```bash
123
191
tree .
@@ -131,6 +199,126 @@ tree .
131
199
└── tsconfig.json
132
200
```
133
201
134
-
The exact results will differ slightly depending on which `app-type` you use, but they will all include a `config.ts` or `config.js` file where the Bee node's API endpoint must be specified.
202
+
The logic for purchasing storage and uploading and downloading data is all contained in the `App.tsx` file:
203
+
204
+
:::tip
205
+
For a step-by-step breakdown of how the code below works, check out the examples section for an explanation of this template along with several others.
After installing and starting the application, you will be first be greeted with a button that will purchase a new postage batch or select an existing one as needed.
278
+
279
+

280
+
281
+
After a postage batch is selected, you will be greeted with an interface for uploading data:
282
+
283
+

284
+
285
+
After selecting a file to upload, a reference hash to the file will be returned:
286
+
287
+

288
+
289
+
Currently our application is running on localhost, and is only accessible locally. To make this application accessible for anyone on Swarm, all we need to do create a production build of our application using `vitebuild` and then upload it to the Swarm with `swarm-cli`.
290
+
291
+
```bash
292
+
npmrunbuild
293
+
```
294
+
295
+
This will generate a production build in the `/dist` directory, which we can than use `swarm-cli` to upload:
296
+
297
+
```bash
298
+
swarm-cliuploaddist
299
+
```
300
+
301
+
`swarm-cli` will prompt us to create or select a postage batch, after which it will automatically detect that we are trying upload a website based on the `index.html` file, use that information to set the `--index-document`, complete the upload, and return a hash to us which can now be used by anyone with a Bee node to access our app:
As a next step, you may wish to look into [connecting your site to an ENS domain](https://docs.ethswarm.org/docs/develop/access-the-swarm/host-your-website/#enable-ens-on-your-node) so that it is accessible from a human-readable address.
135
323
136
-
The endpoint is set to the default Bee API endpoint of `http://localhost:1633`, if your node uses a different endpoint you will need to update it in the config file.
324
+
You may also want to start exploring more the [example applications section (PLACEHOLDER)](#) along with the accompanying step-by-step guides to deepen your understanding of what's possible on Swarm.
0 commit comments