Creating and Managing API Keys

This guide walks you through the process of creating API keys on the Bakery Marketplace. Follow these steps to create and manage the use your API keys.

1. Creating an API Key

Step 1: Log In to Your Account

Visit the Bakery Marketplace and log in with your credentials.

Step 2: Navigate to the API-Key Control

After logging in, you'll be directed to your dashboard. On the left sidebar, locate and select API-Key. This will take you to the API-Key Control page, where you can manage your API keys.

alt text

Step 3: View Your User ID and API Key Status

On the API-Key Control page, you'll see your User ID and the status of any API keys you've created. Ensure the status of your API key is set to "Active" to enable usage.

Step 4: Refresh Your API Key

If you ever need to refresh your API key, click the "Refresh API Key" button. This action will generate a new key while invalidating the old one.

alt text

2. API Keys with Bagelml

Step 1: Install Required Dependencies

Before you can use your API keys, install the following dependencies:

  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

Step 2: Update Your API Configuration

After installing the necessary dependencies, update the api.js file located in node_modules > bageldb-beta > src > api. Replace its contents with the following code to create an API key:

async create_api_key(name, userId, apiKey = '') {
  const headers = {
    'Content-Type': 'application/json'
  }

  try {
    const response = await fetch(this._api_url + '/api_keys', {
      method: 'POST',
      headers,
      body: JSON.stringify({ name, api_key: apiKey, userId })
    })

    const data = await response.json()

    if (response.status === 200) {
      console.log('API key created successfully and sent to user ID:', userId)
      return data
    } else {
      console.error(`Error creating API key: ${JSON.stringify(data)}`)
    }
  } catch (error) {
    console.error('Error creating API key:', error)
  }
}

Step 3: Initiate the Function

You can now create an API key by running the following code in your project:

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

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

const client = new Client(settings);

const userId = "insert userId";

const createApiKey = async (userId) => {
  try {
    const apiKeyDetail = await client.create_api_key("api-key1", userId);
    console.log(apiKeyDetail);
  } catch (error) {
    console.error('Error creating API key:', error);
  }
};

createApiKey(userId);

Step 4: Handling the API Key Response

Upon successful creation, the method will return:

 (JS) % node apikey.js
API key created successfully and sent to user ID: XXXXXXXXXXXXXXX
{ name: 'api-key1', api_key: 'abcdefghijklmopqrstuv' }

Need extra help or just want to connect with other developers? Join our community on Discord for additional support (https://discord.gg/bagelnet) 👾

Last updated