Getting Started with API

Purpose: This API serves as the backend for our MOTI DID Protocol, allowing users to create, read and update MOTI.BIO profile, manage user accounts.

Prerequisites

Development Environment: Set up a development environment with your preferred programming language (e.g, JavaScript).

Ensure you have the necessary libraries installed. For JavaScript, you can use `fetch` or libraries like `axios`.

const axios = require('axios');

Basic Concepts

Endpoint: The URL where the API is accessed.

HTTP Methods:

  • GET: Retrieve data.

  • POST: Send data.

  • PUT: Update data.

  • DELETE: Remove data.

Headers: Additional information sent with the request (e.g., Authorization, Content-Type). For the MOTI API, headers are not used.

Parameters: Data sent with the request, either in the URL for GET requests or in the body for POST/PUT requests.

Base URL: Know the base URL of the API

TaskID and Base URL

After deploying a task on Koii, a TASKID is generated. The TASKID might change when the task is updated.

  • TaskID Example: CqxkAGdno3FDEB3gkcnCKMo2s9jwHnkaMEp8FsQy6qcB

  • Base URL: https://app.moti.bio/task/${TaskID}/

Making API Requests and Handling Responses

GET Request

Retrieve a list of linktree items:

let result;

result = await axios.get(`${BASE_URL}/linktree/list`)
  .then((res) => res.data)
  .catch((error) => console.log(`Error fetching all linktree:`, error));

// Logs the output
console.log(result);

POST Request

Create a new linktree item:

const payload = {
  data: {},
  publicKey: "value1",
  signature: "value2",
  username: "value3",
};

result = await axios.post(`${BASE_URL}/linktree`, payload)
  .then((res) => res.data)
  .catch((error) => console.log(`Error post request:`, error));

// Logs the output
console.log(result);

PUT Request

Update an existing linktree item:

const payload = {
  data: {},
  publicKey: "value1",
  signature: "value2",
  username: "value3",
};

result = await axios.put(`${BASE_URL}/linktree`, payload)
  .then((res) => res.data)
  .catch((error) => console.log(`Error put request:`, error));

// Logs the output
console.log(result);

Endorsements API

POST Request - Create Endorsement

Create a new endorsement:

// Make an endorsement object
const endorsement = {
  issuer: "value1",
  recipient: "value2",
  meta: {
    section: "value3",
    title: "value4",
    hoverText: "value5",
    description: "value6",
    image: "value7",
    koiiImage: "value8",
    publishedDescription: "value9",
    publishedCTAButtonText: "value10",
    publishedCTAButtonHyperlink: "value11",
    sort: "value12",
    issuer_name: "value13",
    endorsementTime: "value14",
  },
  nonce: "value15",
};

// Generate the endorsement ID
const endorsementId = crypto.createHash("sha256")
  .update(JSON.stringify(endorsement))
  .digest("hex");

endorsement.endorsementId = endorsementId;

const signature = bs58Encode(Buffer.from(endorsementId));

const response = await axios.post(`${BASE_URL}/endorsement/create`, {
  signature,
  endorsement,
});

console.log("Add the Endorsement", response.data);

GET Request - Fetch Endorsements

Retrieve endorsements using a public key:

const publicKey = 'your_public_key_here';

result = await axios.get(`${BASE_URL}/endorsement/${publicKey}`)
  .then((res) => res.data)
  .catch((error) => console.log(`Error fetching Endorsements:`, error));

// Logs the output
console.log(result);

Common API Operations

  • Fetching Data (GET): Retrieve information from the API.

  • Sending Data (POST): Submit new data to the API.

  • Updating Data (PUT/PATCH): Modify existing data in the API.

Error Handling

MOTI.BIO uses standard HTTP response codes:

  • 2xx: Success

  • 4xx: Client errors (e.g., missing parameters)

  • 5xx: Server errors (rare)

This guide provides the essentials for interacting with the MOTI API using JavaScript. Understanding these concepts and examples will help you efficiently integrate MOTI functionalities into your applications.

Last updated