QuickStart

In this tutorial, you'll learn how to leverage Bagel's APIs to efficiently manage your assets, enabling streamlined data access and manipulation capabilities.

Prerequisites

To ensure that all the code in this tutorial runs properly, install the following:

  1. Axios : npm install axios

  2. Node-Fetch: npm install node-fetch

  3. Form data: npm install form-data

  4. uuid: npm install uuid

  5. Buffer: npm install buffer

  6. bagelML: npm install bagelML

Client

The Client class is the main interface to the Bagel API. It requires a Settings object to configure connectivity:

import { Settings, Client } from "bageldb-beta";

// Settings config
const settings = new Settings({
  bagel_api_impl: "rest",
  bagel_server_host: "api.bageldb.ai",
});

const client = new Client(settings);

Step 1: Creating an Asset

Before we can begin managing our asset, we need to create a new asset within Bagel. This can be achieved using the POST /api/v1/asset endpoint:

const apiKey = "insert-your-api-key";
const payload = {
  dataset_type: "RAW",
  title: "Insert Asset Name",
  category: "Insert Category",
  details: "Insert Details",
  tags: [],
  userId: "Insert Your User ID",
};

const createAsset = async () => {
  try {
    const asset = await client.create_asset(payload, apiKey);
    console.log(asset);
  } catch (error) {
    console.error("Error creating asset:", error);
  }
};

createAsset();

Step 2: Upload File to Asset

Once you have created an asset, you may need to upload a file to it. This is useful for associating data files with the asset for further processing. You can achieve this using the POST /api/v1/asset/{asset_id}/upload endpoint.

const assetId = "insert your asset Id";
const filePath = "./sample_data.csv";
const apiKey = "insert your api key";

const uploadFile = async () => {
  try {
    const asset = await client.add_file(assetId, filePath, apiKey);
    console.log(asset);
  } catch (error) {
    console.error("Error uploading file:", error);
  }
};

uploadFile();

In this snippet:

  • assetId: The ID of the asset to which the file will be uploaded.

  • filePath: The path to the file you want to upload.

  • apiKey: Your API key for authentication.

Step 3: Managing the Asset

Bagel's APIs provide functionality for managing assets, including retrieving asset information, updating asset properties, and deleting assets when they are no longer needed.

Retrieving Asset Information

To retrieve information about an asset, we can use the GET /api/v1/asset/{asset_id}/info endpoint. This method retrieves details for a specific Asset using the generated "Asset ID". An API key is used to ensure security.

const apiKey = "insert-your-api-key";
const assetId = "insert-your-asset-id";

const getAsset = async () => {
  try {
    const asset = await client.get_asset_by_Id(assetId, apiKey);
    console.log(asset);
  } catch (error) {
    console.error("Error retrieving asset:", error);
  }
};

getAsset();

Step 4: Update Asset

You may need to update the properties of an existing asset to reflect changes in its status or metadata. This can be done using the PUT /api/v1/asset/{asset_id} endpoint.

const apiKey = "insert-your-api-key";
const assetId = "insert-your-asset-id";

const payload = {
  price: 200,
  is_published: true,
  is_purchased: true,
  details: "Updated asset details",
  title: "Updated asset title",
};

const update = async () => {
  try {
    console.log("Sending request with payload:", payload);

    const response = await client.update_asset(assetId, payload, apiKey);

    console.log("Response received:", response);
  } catch (error) {
    console.error("Error updating asset:", error);
  }
};

update();

Step 5: Delete Asset

If an asset is no longer needed, you can delete it using the DELETE /api/v1/asset/{asset_id} endpoint. This method ensures that the asset is removed from the marketplace and is no longer accessible.

const apiKey = "insert your api key";
const assetId = "insert your asset id"; // Replace with actual asset ID

const deleteAsset = async () => {
  try {
    const asset = await client.delete_asset(assetId, apiKey);
    console.log(asset);
  } catch (error) {
    console.error("Error deleting asset:", error);
  }
};

deleteAsset();

By following these steps, you can effectively manage your assets using Bagel's API, enabling you to create, upload, retrieve, update, and delete assets with ease.

Need extra help or just want to connect with other developers? Join our community on Discord for additional support 👾

Last updated