2024-08-15 12:08:48 +02:00
|
|
|
# Codex SDK
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
The Codex SDK provides an API for interacting with the Codex decentralized storage network.
|
|
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
The SDK has a small bundle size and support tree shaking.
|
2024-08-15 12:08:41 +02:00
|
|
|
|
2024-08-15 12:08:51 +02:00
|
|
|
The SDK is currently under early development and the API can change at any time.
|
|
|
|
|
|
2025-03-26 12:21:51 +01:00
|
|
|
## Breaking changes
|
|
|
|
|
|
2025-03-26 18:27:22 +01:00
|
|
|
- Version 0.1.0 introduces [upload strategy](#upload) to support browser and Node JS.
|
2025-03-26 12:21:51 +01:00
|
|
|
|
2024-09-13 19:19:56 +02:00
|
|
|
## How to use
|
|
|
|
|
|
|
|
|
|
### Sync api
|
|
|
|
|
|
|
|
|
|
The easiest way is to use the sync API, but you will not benefit from tree shaking.
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-30 12:53:30 +02:00
|
|
|
import { Codex } from "@codex-storage/sdk-js";
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
or
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-30 12:53:30 +02:00
|
|
|
const { Codex } = require("@codex-storage/sdk-js");
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
2024-09-13 19:19:56 +02:00
|
|
|
To create a Codex instance, provide the REST API url to interact with the Codex client:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const codex = new Codex("http://localhost:3000");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then you can access any module like this:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const marketplace = codex.marketplace;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Async api
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
import { Codex } from "@codex-storage/sdk-js/async";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const { Codex } = require("@codex-storage/sdk-js/async");
|
|
|
|
|
```
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
To create a Codex instance, provide the REST API url to interact with the Codex client:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const codex = new Codex("http://localhost:3000");
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
2024-08-15 12:08:41 +02:00
|
|
|
To use a module, you need to use the await syntax. If the module is not loaded yet, it will be imported first and then cached in memory.
|
|
|
|
|
|
|
|
|
|
```js
|
2024-09-13 19:25:02 +02:00
|
|
|
const marketplace = await codex.marketplace;
|
2024-08-15 12:08:41 +02:00
|
|
|
```
|
|
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
### Error handling
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
The SDK provides a type called `SafeValue` for error handling instead of throwing errors. It is inspired by Go's "error as value" concept.
|
|
|
|
|
If the value represents an error, `error` is true and `data` will contain the error.
|
|
|
|
|
If the value is not an error, `error` is false and `data` will contain the requested data.
|
|
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
The [CodexError](./src/errors/errors.ts#L16) contains a message and 3 optionals properties:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
- `code`: The (http) code error when it comes from a request
|
|
|
|
|
- `errors`: A {ValidationError} array when it comes from an object validation process
|
|
|
|
|
- `stack`: The error stack when the CodexError results from a error thrown
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const slots = marketplace.activeSlots();
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
if (slots.error) {
|
2024-08-15 12:08:48 +02:00
|
|
|
// Do something to handle the error in slots.data
|
|
|
|
|
return;
|
2024-08-15 12:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Access the slots within slots.data.
|
|
|
|
|
```
|
|
|
|
|
|
2025-02-10 23:04:00 +01:00
|
|
|
### Compatibility
|
|
|
|
|
|
|
|
|
|
| SDK version | Codex version | Codex app |
|
|
|
|
|
| ----------- | ------------- | --------- |
|
|
|
|
|
| latest | master | latest |
|
2025-02-25 23:18:29 +01:00
|
|
|
| 0.0.22 | Testnet 0.2.0 | 0.0.14 |
|
2025-02-10 23:04:00 +01:00
|
|
|
| 0.0.16 | Testnet 0.1.9 | 0.0.13 |
|
|
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
### Marketplace
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
The following API assume that you have already a marketplace module loaded, example:
|
2024-08-15 12:08:41 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const codex = new Codex("http://localhost:3000");
|
|
|
|
|
const marketplace = await codex.marketplace();
|
2024-08-15 12:08:41 +02:00
|
|
|
```
|
|
|
|
|
|
2024-08-15 12:08:15 +02:00
|
|
|
#### activeSlots()
|
|
|
|
|
|
|
|
|
|
Returns active slots.
|
|
|
|
|
|
2025-02-25 21:31:28 +01:00
|
|
|
- returns Promise<[CodexSlot](./src/marketplace/types.ts#L85)[]>
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const slots = await marketplace.activeSlots();
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### activeSlot(slotId)
|
|
|
|
|
|
|
|
|
|
Returns active slot with id {slotId} for the host.
|
|
|
|
|
|
|
|
|
|
- slotId (string, required)
|
2025-02-25 21:31:28 +01:00
|
|
|
- returns Promise<[CodexSlot](./src/marketplace/types.ts#L85)[]>
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const slotId = "AB9........";
|
|
|
|
|
const slot = await marketplace.activeSlot(slotId);
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### availabilities
|
|
|
|
|
|
|
|
|
|
Returns storage that is for sale.
|
|
|
|
|
|
2025-02-25 21:31:28 +01:00
|
|
|
- returns Promise<[CodexAvailability](./src/marketplace/types.ts#L99)>
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const availabilities = await marketplace.availabilities();
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### createAvailability
|
|
|
|
|
|
|
|
|
|
Offers storage for sale.
|
|
|
|
|
|
2025-02-25 21:31:28 +01:00
|
|
|
- input ([CodexCreateAvailabilityInput](./src/marketplace/types.ts#L175), required)
|
|
|
|
|
- returns Promise<[CodexAvailabilityCreateResponse](./src/marketplace/types.ts#L186)[]>
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:41 +02:00
|
|
|
const response = await marketplace.createAvailability({
|
2025-03-04 11:07:05 +01:00
|
|
|
totalCollateral: 1,
|
2024-08-15 12:08:48 +02:00
|
|
|
totalSize: 3000,
|
2025-03-04 11:07:05 +01:00
|
|
|
minPricePerBytePerSecond: 100,
|
2024-08-15 12:08:48 +02:00
|
|
|
duration: 100,
|
|
|
|
|
});
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
2024-09-20 10:50:43 +02:00
|
|
|
#### updateAvailability
|
|
|
|
|
|
|
|
|
|
Updates availability.
|
|
|
|
|
|
2025-02-25 21:31:28 +01:00
|
|
|
- input ([CodexUpdateAvailabilityInput](./src/marketplace/types.ts#L186), required)
|
2024-09-20 10:50:43 +02:00
|
|
|
- returns Promise<"">
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const response = await marketplace.updateAvailability({
|
|
|
|
|
id: "0x.....................",
|
2025-03-04 11:07:05 +01:00
|
|
|
totalCollateral: 1,
|
2024-09-20 10:50:43 +02:00
|
|
|
totalSize: 3000,
|
2025-03-04 11:07:05 +01:00
|
|
|
minPricePerBytePerSecond: 100,
|
2024-09-20 10:50:43 +02:00
|
|
|
duration: 100,
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2024-08-15 12:08:15 +02:00
|
|
|
#### reservations
|
|
|
|
|
|
|
|
|
|
Return list of reservations for ongoing Storage Requests that the node hosts.
|
|
|
|
|
|
|
|
|
|
- availabilityId (string, required)
|
2025-02-25 21:31:28 +01:00
|
|
|
- returns Promise<[CodexReservation](./src/marketplace/types.ts#L198)[]>
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const reservations = await marketplace.reservations("Ox...");
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### createStorageRequest
|
|
|
|
|
|
|
|
|
|
Creates a new Request for storage
|
|
|
|
|
|
2025-02-25 21:31:28 +01:00
|
|
|
- input ([CodexCreateStorageRequestInput](./src/marketplace/types.ts#L230), required)
|
2024-08-30 12:24:00 +02:00
|
|
|
- returns Promise<string>
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:41 +02:00
|
|
|
const request = await marketplace.createStorageRequest({
|
2024-08-15 12:08:48 +02:00
|
|
|
duration: 3000,
|
2025-02-07 18:28:03 +01:00
|
|
|
pricePerBytePerSecond: 1,
|
2024-08-15 12:08:48 +02:00
|
|
|
proofProbability: 1,
|
|
|
|
|
nodes: 1,
|
|
|
|
|
tolerance: 0,
|
2025-02-10 22:50:06 +01:00
|
|
|
collateralPerByte: 100,
|
2024-08-15 12:08:48 +02:00
|
|
|
expiry: 3000,
|
|
|
|
|
});
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### purchaseIds
|
|
|
|
|
|
|
|
|
|
Returns list of purchase IDs
|
|
|
|
|
|
|
|
|
|
- returns Promise<string[]>
|
|
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const ids = await marketplace.purchaseIds();
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### purchaseDetail
|
|
|
|
|
|
2024-08-15 12:08:33 +02:00
|
|
|
Returns purchase details
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
- purchaseId (string, required)
|
2025-02-25 21:31:28 +01:00
|
|
|
- returns Promise<[CodexPurchase](./src/marketplace/types.ts#L214)[]>
|
2024-08-15 12:08:15 +02:00
|
|
|
|
2024-08-15 12:08:48 +02:00
|
|
|
Example:
|
2024-08-15 12:08:15 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-08-15 12:08:48 +02:00
|
|
|
const purchaseId = "Ox........";
|
|
|
|
|
const purchase = await marketplace.purchaseDetail(purchaseId);
|
2024-08-15 12:08:15 +02:00
|
|
|
```
|
2024-08-30 12:24:00 +02:00
|
|
|
|
|
|
|
|
### Data
|
|
|
|
|
|
|
|
|
|
The following API assume that you have already a data module loaded, example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const codex = new Codex("http://localhost:3000");
|
2024-09-13 19:25:02 +02:00
|
|
|
const data = await codex.data;
|
2024-08-30 12:24:00 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### cids
|
|
|
|
|
|
|
|
|
|
Returns the manifest stored locally in node.
|
|
|
|
|
|
2025-02-25 21:31:28 +01:00
|
|
|
- returns Promise<[CodexDataResponse](./src/data/types.ts#L54)[]>
|
2024-08-30 12:24:00 +02:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const cids = await data.cids();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### space
|
|
|
|
|
|
|
|
|
|
Returns a summary of the storage space allocation of the node
|
|
|
|
|
|
2025-03-26 18:30:33 +01:00
|
|
|
- returns Promise<[CodexNodeSpace](./src/data/types.ts#L56)[]>
|
2024-08-30 12:24:00 +02:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const space = await data.space();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### upload
|
|
|
|
|
|
|
|
|
|
Upload a file in a streaming manner
|
|
|
|
|
|
2025-03-26 18:27:22 +01:00
|
|
|
#### Browser
|
2025-03-26 12:21:51 +01:00
|
|
|
|
2025-03-26 12:31:10 +01:00
|
|
|
- stategy [BrowserUploadStategy](./src/data/browser-upload.ts#L5)
|
2025-03-26 12:21:51 +01:00
|
|
|
- returns [UploadResponse](./src/data/types.ts#L80)
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const file = new File(["foo"], "foo.txt", { type: "text/plain" });
|
|
|
|
|
|
|
|
|
|
const onProgress = (loaded, total) => {
|
|
|
|
|
console.info("Loaded", loaded, "total", total);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const metadata = { filename: "foo.xt", mimetype: "text/plain" };
|
|
|
|
|
|
2025-03-26 12:31:10 +01:00
|
|
|
const stategy = new BrowserUploadStategy(file, onProgress, metadata);
|
2025-03-26 12:21:51 +01:00
|
|
|
|
|
|
|
|
const uploadResponse = data.upload(stategy);
|
|
|
|
|
|
|
|
|
|
const res = await uploadResponse.result;
|
|
|
|
|
|
|
|
|
|
if (res.error) {
|
|
|
|
|
console.error(res.data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.info("CID is", res.data);
|
|
|
|
|
```
|
|
|
|
|
|
2025-03-26 18:27:22 +01:00
|
|
|
#### Node
|
2025-03-26 12:21:51 +01:00
|
|
|
|
2025-03-26 12:31:10 +01:00
|
|
|
- stategy [NodeUploadStategy](./src/data/node-download.ts#L8)
|
2025-02-25 21:31:28 +01:00
|
|
|
- returns [UploadResponse](./src/data/types.ts#L80)
|
2024-08-30 12:24:00 +02:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
2025-03-26 12:31:10 +01:00
|
|
|
const stategy = new NodeUploadStategy("Hello World !");
|
2025-03-26 12:21:51 +01:00
|
|
|
const uploadResponse = data.upload(stategy);
|
|
|
|
|
|
|
|
|
|
const res = await uploadResponse.result;
|
|
|
|
|
|
|
|
|
|
if (res.error) {
|
|
|
|
|
console.error(res.data);
|
|
|
|
|
return;
|
2024-11-01 18:02:05 +01:00
|
|
|
}
|
2025-03-26 12:21:51 +01:00
|
|
|
|
|
|
|
|
console.info("CID is", res.data);
|
2024-08-30 12:24:00 +02:00
|
|
|
```
|
|
|
|
|
|
2024-10-23 16:53:40 +02:00
|
|
|
#### manifest
|
|
|
|
|
|
|
|
|
|
Download only the dataset manifest from the network to the local node if it's not available locally.
|
|
|
|
|
|
|
|
|
|
- cid (string, required)
|
2024-11-01 17:33:40 +01:00
|
|
|
- returns [CodexManifest](./src/data/types.ts#L3)
|
2024-10-23 16:53:40 +02:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const cid = "QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N";
|
|
|
|
|
const manifest = await data.fetchManifest(cid);
|
|
|
|
|
```
|
|
|
|
|
|
2024-10-25 11:10:13 +02:00
|
|
|
#### networkDownloadStream
|
|
|
|
|
|
|
|
|
|
Download a file from the network in a streaming manner.
|
|
|
|
|
If the file is not available locally, it will be retrieved from other nodes in the network if able.
|
|
|
|
|
|
|
|
|
|
- cid (string, required)
|
2024-10-25 11:36:51 +02:00
|
|
|
- returns Response
|
2024-10-25 11:10:13 +02:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const cid = "QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N";
|
|
|
|
|
const result = await data.networkDownloadStream(cid);
|
|
|
|
|
```
|
|
|
|
|
|
2024-10-25 13:28:27 +02:00
|
|
|
#### localDownload
|
|
|
|
|
|
|
|
|
|
Download a file from the local node in a streaming manner.
|
|
|
|
|
If the file is not available locally, a 404 is returned.
|
|
|
|
|
|
|
|
|
|
- cid (string, required)
|
|
|
|
|
- returns Response
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const cid = "QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N";
|
|
|
|
|
const result = await data.localDownload(cid);
|
|
|
|
|
```
|
|
|
|
|
|
2024-09-13 19:25:02 +02:00
|
|
|
### Debug
|
2024-08-30 12:24:00 +02:00
|
|
|
|
|
|
|
|
The following API assume that you have already a node module loaded, example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const codex = new Codex("http://localhost:3000");
|
2024-09-13 19:25:02 +02:00
|
|
|
const data = await codex.debug;
|
2024-08-30 12:24:00 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### setLogLevel
|
|
|
|
|
|
|
|
|
|
Set log level at run time.
|
|
|
|
|
|
|
|
|
|
- level ([CodexLogLevel](./src/debug/types.ts#L3), required)
|
2024-09-13 19:19:56 +02:00
|
|
|
- returns Promise<"">
|
2024-08-30 12:24:00 +02:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
await debug.setLogLevel("DEBUG");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### info
|
|
|
|
|
|
|
|
|
|
Gets node information
|
|
|
|
|
|
|
|
|
|
- returns Promise<[CodexDebugInfo](./src/debug/types.ts#L15)>
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const info = await debug.info();
|
|
|
|
|
```
|
2024-09-13 19:25:02 +02:00
|
|
|
|
|
|
|
|
### Node
|
|
|
|
|
|
|
|
|
|
The following API assume that you have already a node module loaded, example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const codex = new Codex("http://localhost:3000");
|
|
|
|
|
const node = await codex.node;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### spr
|
|
|
|
|
|
|
|
|
|
Get Node's SPR
|
|
|
|
|
|
2024-09-13 22:30:37 +02:00
|
|
|
- returns Promise<[CodexSpr](./src/node/types.ts#L1)>
|
2024-09-13 19:25:02 +02:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const spr = await node.spr();
|
|
|
|
|
```
|