Skip to content

Managed wallet API

Anil Murty edited this page Apr 11, 2025 · 6 revisions

Lease Flow Example Script

This guide demonstrates how to create and manage a deployment with a lease using the Akash Console API and an API key.

⚠️🛠️WIP🛠️⚠️ Managed wallet API is still under development, so the following document will change frequently in the near future until we officially release the first version.

Prerequisites

  • An API key from the Akash Console
  • Node.js installed on your system (it can be any other language)

Create an API key

image image

Script Workflow

The full example script can be found here

The script executes the following steps in sequence:

  1. Certificate Creation

    • Creates a new certificate for secure communication
    • Returns certPem and encryptedKey for use in lease creation
  2. Deployment Creation

    • Creates a deployment using the provided SDL file
    • Uses a default deposit of 5$
    • Returns deployment sequence ID (dseq) and manifest
  3. Bid Collection

    • Waits for and collects bids from providers
    • Polls the API every 3 seconds (max 10 attempts)
    • Selects the first available bid
  4. Lease Creation

    • Creates a lease using the selected bid
    • Sends the deployment manifest
    • Includes certificate information for secure communication
  5. Deposit Deployment

    • Deposit in the deployment of 0.5$
  6. Deployment Closure

    • Closes the deployment after lease creation

API Endpoints and Code Samples

You can do this flow with any language, however in this example we'll be using nodejs.

Common HTTP Headers

All requests require the following headers:

{
  "Content-Type": "application/json",
  "x-api-key": "your-api-key-here"
}

Axios Configuration

const api = axios.create({
  baseURL: "https://console-api.akash.network",
  headers: {
    "Content-Type": "application/json"
  }
});

1. Create Certificate

// Response Type
interface CertificateResponse {
  data: {
    data: {
      certPem: string;      // Certificate in PEM format
      encryptedKey: string; // Encrypted private key
    }
  }
}

// API Call
const certResponse = await api.post<CertificateResponse>(
  "/v1/certificates",
  {},  // Empty request body
  {
    headers: {
      "x-api-key": apiKey
    }
  }
);

2. Create Deployment

// Request Type
interface CreateDeploymentRequest {
  data: {
    sdl: string;     // SDL file content
    deposit: number; // Amount in dollars with minimum 5$
  }
}

// Response Type
interface CreateDeploymentResponse {
  data: {
    data: {
      dseq: string;     // Deployment sequence ID
      manifest: string; // Deployment manifest
    }
  }
}

// API Call
const deployResponse = await api.post<CreateDeploymentResponse>(
  "/v1/deployments",
  {
    data: {
      sdl: yml,
      deposit: 5
    }
  } as CreateDeploymentRequest,
  {
    headers: {
      "x-api-key": apiKey
    }
  }
);

3. Fetch Bids

// Response Type
interface BidID {
  owner: string;
  dseq: string;
  gseq: number;
  oseq: number;
  provider: string;
}

interface Bid {
  bid_id: BidID;
  state: string;
  price: {
    denom: string;
    amount: string;
  };
  created_at: string;
}

interface BidsResponse {
  data: {
    data: {
      bid: Bid;
    }[];
  }
}

// API Call
async function waitForBids(dseq: string, apiKey: string, maxAttempts = 10): Promise<Bid[]> {
  for (let i = 0; i < maxAttempts; i++) {
    const response = await api.get<BidsResponse>(`/v1/bids?dseq=${dseq}`, {
      headers: {
        "x-api-key": apiKey
      }
    });

    if (response.data?.data?.length > 0) {
      return response.data.data;
    }
    await new Promise(resolve => setTimeout(resolve, 3000));
  }
  throw new Error("No bids received after maximum attempts");
}

4. Create Lease

// Request Type
interface CreateLeaseRequest {
  manifest: string;
  certificate: {
    certPem: string;
    keyPem: string;
  };
  leases: {
    dseq: string;
    gseq: number;
    oseq: number;
    provider: string;
  }[];
}

interface CreateLeaseResponse {
  data: {
    deployment: {
      deployment_id: {
        owner: string;
        dseq: string;
      };
      state: string;
      version: string;
      created_at: string;
    };
    leases: {
      lease_id: {
        owner: string;
        dseq: string;
        gseq: number;
        oseq: number;
        provider: string;
      };
      state: string;
      price: {
        denom: string;
        amount: string;
      };
      created_at: string;
      closed_on: string;
    }[];
    escrow_account: {
      id: {
        scope: string;
        xid: string;
      };
      owner: string;
      state: string;
      balance: {
        denom: string;
        amount: string;
      };
      transferred: {
        denom: string;
        amount: string;
      };
      settled_at: string;
      depositor: string;
      funds: {
        denom: string;
        amount: string;
      };
    };
  }
}

// API Call
const leaseResponse = await api.post<CreateLeaseResponse>(
  "/v1/leases",
  {
    manifest,
    certificate: {
      certPem,
      keyPem: encryptedKey
    },
    leases: [
      {
        dseq,
        gseq: firstBid.bid.bid_id.gseq,
        oseq: firstBid.bid.bid_id.oseq,
        provider: firstBid.bid.bid_id.provider
      }
    ]
  } as CreateLeaseRequest,
  {
    headers: {
      "x-api-key": apiKey
    }
  }
);

5. Deposit Deployment

// Request Type
interface DepositDeploymentRequest {
  data: {
    deposit: number;
    dseq: string;
  }
}

// Response Type
interface DepositDeploymentResponse {
  data: CreateLeaseResponse
}

// API Call
const depositResponse = await api.delete<DepositDeploymentResponse>(
  `/v1/deposit-deployment`,
  {
    data: {
      dseq,
      deposit: 0.5
    }    
  } as DepositDeploymentRequest,
  {
    headers: {
      "x-api-key": apiKey
    }
  }
);

6. Close Deployment

// Response Type
interface CloseDeploymentResponse {
  data: {
    data: {
      status: string;
      message: string;
    }
  }
}

// API Call
const closeResponse = await api.delete<CloseDeploymentResponse>(
  `/v1/deployments/${dseq}`,
  {
    headers: {
      "x-api-key": apiKey
    }
  }
);

Related Resources