Skip to content

Commit baf6677

Browse files
committed
getting started page and template app revisions
1 parent 93f87f8 commit baf6677

File tree

4 files changed

+208
-20
lines changed

4 files changed

+208
-20
lines changed

docs/documentation/getting-started.md

Lines changed: 208 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Tabs from '@theme/Tabs'
88
import TabItem from '@theme/TabItem'
99

1010

11-
1211
## About *bee-js*
1312

1413
`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.
@@ -46,9 +45,11 @@ yarn add @ethersphere/bee-js --save
4645
</TabItem>
4746
</Tabs>
4847

49-
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).
5049

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+
:::
5253

5354
```js
5455
import { Bee } from "@ethersphere/bee-js"
@@ -58,10 +59,6 @@ const bee = new Bee('http://localhost:1633')
5859

5960
That’s it! now you can use the `bee` object.
6061

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-
6562
:::tip Using `<script>` import
6663

6764
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`:
7673
:::
7774

7875

79-
## Quickstart With *create-swarm-app*
76+
## Quickstart with *create-swarm-app*
8077

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

83-
:::note WSL WARNING
80+
:::warning WSL WARNING
8481
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.
8582

8683
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.
8784
:::
8885

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
9087

9188
```bash
92-
npm init swarm-app@latest app-name app-type
89+
npm init swarm-app@latest <app-name> <app-type>
9390
```
9491

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)
98+
99+
### 2) Back‑end templates (`node`, `node-esm`, `node-ts`)
96100

97-
Start a Swarm project using TypeScript:
101+
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):
98104

99105
```bash
100106
npm init swarm-app@latest my-dapp node-ts
107+
108+
# then
109+
cd my-dapp
110+
npm install
111+
npm start
101112
```
102113

103-
Your project structure will look like:
114+
Project structure:
104115

105-
```bash
116+
```text
106117
.
107118
├── package.json
108119
├── src
@@ -111,13 +122,70 @@ Your project structure will look like:
111122
└── tsconfig.json
112123
```
113124

114-
or using Vite and TypeScript:
125+
`src/config.ts` — Bee API endpoint (adjust if your node is not on the default):
126+
127+
```ts
128+
export const BEE_HOST = 'http://localhost:1633'
129+
```
130+
131+
`src/index.ts` — minimal end‑to‑end flow (init Bee, get/create a stamp, upload, download):
132+
133+
```ts
134+
import { Bee } from '@ethersphere/bee-js'
135+
import { BEE_HOST } from './config'
136+
137+
main()
138+
139+
async function main() {
140+
const bee = new Bee(BEE_HOST)
141+
const batchId = await getOrCreatePostageBatch(bee)
142+
console.log('Batch ID', batchId.toString())
143+
144+
const data = 'Hello, world! The current time is ' + new Date().toLocaleString()
145+
const uploadResult = await bee.uploadData(batchId, data)
146+
console.log('Swarm hash', uploadResult.reference.toHex())
147+
148+
const downloadResult = await bee.downloadData(uploadResult.reference)
149+
console.log('Downloaded data:', downloadResult.toUtf8())
150+
}
151+
152+
async function getOrCreatePostageBatch(bee: Bee) {
153+
const batches = await bee.getPostageBatches()
154+
const usable = batches.find(x => x.usable)
155+
156+
if (usable) {
157+
return usable.batchID
158+
} else {
159+
// amount and depth are examples; tune for your needs
160+
return bee.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!
115172

116173
```bash
117174
npm init swarm-app@latest my-dapp vite-tsx
118175
```
119176

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:
121189

122190
```bash
123191
tree .
@@ -131,6 +199,126 @@ tree .
131199
└── tsconfig.json
132200
```
133201

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.
206+
:::
207+
208+
```typescript
209+
import { BatchId, Bee } from '@ethersphere/bee-js'
210+
import { useState } from 'react'
211+
import { BEE_HOST } from './config'
212+
213+
export function App() {
214+
const [batchId, setBatchId] = useState<BatchId | null>(null)
215+
const [file, setFile] = useState<File | null>(null)
216+
const [fileList, setFileList] = useState<FileList | null>(null)
217+
const [swarmHash, setSwarmHash] = useState<string | null>(null)
218+
219+
const bee = new Bee(BEE_HOST)
220+
221+
async function getOrCreatePostageBatch() {
222+
const batches = await bee.getPostageBatches()
223+
const usable = batches.find(x => x.usable)
224+
225+
if (usable) {
226+
setBatchId(usable.batchID)
227+
} else {
228+
setBatchId(await bee.createPostageBatch('500000000', 20))
229+
}
230+
}
231+
232+
async function uploadFile() {
233+
if (!batchId) {
234+
return
235+
}
236+
const result = await bee.uploadFile(batchId, file)
237+
setSwarmHash(result.reference.toHex())
238+
setFile(null)
239+
}
240+
241+
async function uploadDirectory() {
242+
if (!batchId || !fileList) {
243+
return
244+
}
245+
const result = await bee.uploadFiles(batchId, fileList)
246+
setSwarmHash(result.reference.toHex())
247+
setFileList(null)
248+
}
249+
250+
const directoryInputAttributes = {
251+
webkitdirectory: '',
252+
directory: '',
253+
multiple: true
254+
}
255+
256+
return (
257+
<div>
258+
{!batchId && <button onClick={getOrCreatePostageBatch}>Get or create postage batch</button>}
259+
{batchId && <p>Batch ID: {batchId.toHex()}</p>}
260+
{batchId && !swarmHash && (
261+
<div>
262+
<p>Single file upload</p>
263+
<input type="file" onChange={e => setFile(e.target.files![0])} />
264+
<button onClick={uploadFile}>Upload file</button>
265+
266+
<p>Directory upload</p>
267+
<input type="file" onChange={e => setFileList(e.target.files)} {...directoryInputAttributes} />
268+
<button onClick={uploadDirectory}>Upload directory</button>
269+
</div>
270+
)}
271+
{swarmHash && <a href={BEE_HOST + '/bzz/' + swarmHash}>Swarm hash: {swarmHash}</a>}
272+
</div>
273+
)
274+
}
275+
```
276+
277+
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+
![](/img/develop-on-swarm-00.jpg)
280+
281+
After a postage batch is selected, you will be greeted with an interface for uploading data:
282+
283+
![](/img/develop-on-swarm-01.jpg)
284+
285+
After selecting a file to upload, a reference hash to the file will be returned:
286+
287+
![](/img/develop-on-swarm-02.jpg)
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 `vite build` and then upload it to the Swarm with `swarm-cli`.
290+
291+
```bash
292+
npm run build
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-cli upload dist
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:
302+
303+
```bash
304+
? Please select a stamp for this action
305+
4996787aee78da46b6e32d8141aee89ebb4f2ef3301bf04e0a399247fc414f27 550.296 MB
306+
Setting --index-document to index.html
307+
Swarm hash: 764b08bb0f9e82d4bdce951b1ded816bd0417e039828e4308d61ab3035ff60a2
308+
URL: http://localhost:1633/bzz/764b08bb0f9e82d4bdce951b1ded816bd0417e039828e4308d61ab3035ff60a2/
309+
Stamp ID: 4996787a
310+
Usage: 13%
311+
Capacity (immutable): 550.296 MB remaining out of 628.910 MB
312+
```
313+
314+
The URL returned in the terminal can now be shared and accessed by anyone with a Bee node:
315+
316+
```bash
317+
http://localhost:1633/bzz/764b08bb0f9e82d4bdce951b1ded816bd0417e039828e4308d61ab3035ff60a2/
318+
```
319+
320+
## Next Steps
321+
322+
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.
135323
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.

static/img/develop-on-swarm-00.jpg

28.2 KB
Loading

static/img/develop-on-swarm-01.jpg

107 KB
Loading

static/img/develop-on-swarm-02.jpg

90.8 KB
Loading

0 commit comments

Comments
 (0)