Bako Safe SDK
Provider
Services

Provider Services

The BakoProvider uses an internal Service layer for API communication. Understanding these services helps with debugging and advanced usage.

Service Architecture

BakoProvider
    └── Service
        ├── Authentication
        ├── Predicates
        ├── Transactions
        └── Workspaces

Service Class

The Service class handles all HTTP communication with the Bako API.

Static Methods

create()

Create a new user session.

static async create(
  params: IAuthRequestCode,
  api?: string
): Promise<IAuthCreateResponse>

Request:

interface IAuthRequestCode {
  address: string;
  provider: string;
  type?: TypeUser;
}

Response:

interface IAuthCreateResponse {
  code: string;      // Challenge to sign
  validAt: string;   // Expiration timestamp
}

sign()

Authenticate with signed challenge.

static async sign(
  params: IAuthSignRequest,
  api?: string
): Promise<IAuthSignResponse>

Request:

interface IAuthSignRequest {
  digest: string;     // The challenge code
  encoder: TypeUser;  // Signature type
  signature: string;  // Signed challenge
}

Response:

interface IAuthSignResponse {
  accessToken: string;
  address: string;
  avatar: string;
  user_id: string;
  workspace: Workspace;
}

cliAuth()

Authenticate with API token.

static async cliAuth(
  params: CLIAuthPayload
): Promise<CLIAuth>

Request:

interface CLIAuthPayload {
  token: string;
  network: {
    url: string;
    chainId: string;
  };
  serverApi?: string;
}

API Endpoints

MethodEndpointDescription
POST/auth/createGenerate challenge
POST/auth/signAuthenticate with signature
POST/cli/authAuthenticate with API token
GET/workspaceList workspaces
POST/predicateCreate predicate
GET/predicate/:addressGet predicate by address
POST/transactionCreate transaction
GET/transaction/:hashGet transaction
POST/transaction/signSign transaction
POST/transaction/sendSend transaction

Request Headers

All authenticated requests include:

enum AuthRequestHeaders {
  Authorization = 'Authorization',
  Signeraddress = 'Signeraddress'
}
 
// Example headers
{
  'Authorization': 'Bearer <token>',
  'Signeraddress': '<user-address>'
}

Service Configuration

Default Configuration

const defaultConfig = {
  serverUrl: 'https://api.bako.global'
};

Custom Configuration

interface ApiConfigurable {
  address?: string;
  token?: string;
  serverUrl?: string;
}

Error Responses

The service handles various error types:

StatusMeaning
400Bad request / Invalid parameters
401Invalid or expired token
403Insufficient permissions
404Resource not found
500Server error

Error Handling Example

try {
  const result = await provider.createTransaction(params);
} catch (error) {
  if (error.response) {
    switch (error.response.status) {
      case 401:
        // Re-authenticate
        break;
      case 403:
        // Check permissions
        break;
      default:
        console.error('API Error:', error.response.data);
    }
  }
}

Direct Service Usage

For advanced use cases, you can use the Service directly:

import { Service } from 'bakosafe';
 
// Create session
const authResponse = await Service.create({
  address: walletAddress,
  provider: networkUrl,
  type: TypeUser.FUEL
});
 
// Sign
const signResponse = await Service.sign({
  digest: authResponse.code,
  encoder: TypeUser.FUEL,
  signature: walletSignature
});

Next Steps