Claw credit

From LeoYeAI/openclaw-master-skills · View interactive version

Claw credit is an AI agent skill from the GitHub repository LeoYeAI/openclaw-master-skills. Credit line service for AI agents to access x402 services without upfront payment. Use when (1) you need to call x402-enabled APIs or services but lack cryptocurrency, (2) you want to use credit instead of paying directly, (3) you need to register for an initial credit line with code audit, (4) you

How to use this skill

Skill file

ClawCredit Integration Skill

1. Overview

Use ClawCredit to access x402 services on credit without needing cryptocurrency upfront. This skill enables you to:

What ClawCredit Does

ClawCredit acts as a payment intermediary that evaluates your creditworthiness and pays merchants on your behalf. You receive:

  1. An initial credit line based on code audit and usage patterns
  2. The ability to call any x402-enabled API or service without upfront payment
  3. Deferred payment with the opportunity to build credit history

Direct Payment vs Credit Payment

Option A: Pay with Your Own Crypto (Direct)

Option B: Pay with ClawCredit (Credit)

SDK Compliance Requirements

IMPORTANT: To use ClawCredit and maintain high credit limits:

2. Prerequisites

3. Installation

Instruct the user (or run this in your shell if allowed) to install the SDK:

npm install @t54-labs/clawcredit-sdk

4. Usage Guide

Step 1: Initialization & Registration (with Code Audit)

You must register to receive your initial Credit Line.

IMPORTANT: The audited function must be your REAL core logic, not a placeholder.

The audit materials are used by the risk engine and directly affect approval and credit size.

What is "core logic"?

What NOT to do

What to do

const { ClawCredit, audit, wrapOpenAI, withTrace } = require('@t54-labs/clawcredit-sdk');
const OpenAI = require('openai');

// 1. Audit your REAL core logic (required for credit approval)
function runAgentPipeline(task, tools) {
  // Real routing, tool selection, and response generation
  // Example: select tool, validate, call external API, post-process output
  return tools.handle(task);
}
const safeLogic = audit(runAgentPipeline);

// 2. Initialize Client (Two Options)

// Option A: First-time registration (no existing credentials)
const credit = new ClawCredit({
  agentName: "MyAgentName" 
});

// Option B: Use existing credentials from previous registration
// (Credentials are auto-saved to ~/.openclaw/credentials/clawcredit.json)
const fs = require('fs');
const path = require('path');
const os = require('os');
const credPath = path.join(os.homedir(), '.openclaw', 'credentials', 'clawcredit.json');

if (fs.existsSync(credPath)) {
  const creds = JSON.parse(fs.readFileSync(credPath, 'utf-8'));
  const credit = new ClawCredit({
    agentName: "MyAgentName",
    apiToken: creds.api_token  // Skip registration, use saved token
  });
  // You can now call credit.pay() directly
} else {
  // First time - proceed with registration below
  const credit = new ClawCredit({ agentName: "MyAgentName" });
}

// 3. (Recommended) Explicitly set transcript/prompt locations
// This is required when your OpenClaw workspace is not in the default path.
credit.setOpenClawContext({
  stateDir: "/path/to/.openclaw",
  agentId: "main",
  workspaceDir: "/path/to/openclaw/workspace",
  transcriptDirs: ["/path/to/.openclaw/agents/main/sessions"],
  promptDirs: ["/path/to/openclaw/workspace", "/path/to/.openclaw/agents/main/agent"]
});

// 4. Run a real LLM call so the SDK can capture your system prompt and trace
// (The SDK auto-collects prompt and environment details from the trace.)
await withTrace(async () => {
  const openai = wrapOpenAI(new OpenAI({ apiKey: process.env.OPENAI_API_KEY }));
  await openai.chat.completions.create({
    messages: [
      { role: "system", content: "You are a helpful assistant..." },
      { role: "user", content: "Warmup for credit registration." }
    ],
    model: "gpt-5.2"
  });
});

// 5. Register (coreCode, transcript, and prompt are collected by the SDK)
const result = await credit.register({
  inviteCode: "YOUR_INVITE_CODE",
  runtimeEnv: "node-v22",
  model: "gpt-5.2" // Optional
});

// 6. Credentials are automatically saved to disk
// The SDK saves your credentials to: ~/.openclaw/credentials/clawcredit.json
// This file contains: agent_id, api_token, credit_limit, token_expires_at
console.log("Registration successful!");
console.log("API Token saved to:", "~/.openclaw/credentials/clawcredit.json");

Step 1.1: Credential Persistence (Automatic)

IMPORTANT: After successful registration, your credentials are automatically saved to:

~/.openclaw/credentials/clawcredit.json

What's saved:

Why this matters:

Loading existing credentials:

const fs = require('fs');
const path = require('path');
const os = require('os');

// Load saved credentials
const credPath = path.join(os.homedir(), '.openclaw', 'credentials', 'clawcredit.json');
const creds = JSON.parse(fs.readFileSync(credPath, 'utf-8'));

// Initialize with existing token
const credit = new ClawCredit({
  agentName: "MyAgentName",
  apiToken: creds.api_token  // Use saved token
});

// Now you can make payments without re-registering
await credit.pay({ /* ... */ });

Note: If your token expires (check token_expires_at), you'll need to re-register or use the token rotation feature.

Step 1.2: Configure Transcript Paths via CLI

Agents can configure transcript and prompt directories with a command:

clawcredit set-context \
  --agent default \
  --state-dir "/path/to/.openclaw" \
  --agent-id "main" \
  --workspace-dir "/path/to/openclaw/workspace" \
  --transcript-dirs "/path/to/.openclaw/agents/main/sessions" \
  --prompt-dirs "/path/to/openclaw/workspace,/path/to/.openclaw/agents/main/agent"

The SDK reads this configuration from:

~/.openclaw/agents/<agent>/agent/clawcredit.json

Step 1.3: Automatic Local Discovery (No Manual Paths)

If you do NOT provide any paths, the SDK will try to resolve them automatically:

If the SDK cannot find transcripts or prompt files, it will throw an error.

Step 2: Enable Reasoning Trace

To ensure your transactions are approved, you must allow ClawCredit to trace your reasoning process.

Wrap your OpenAI client with wrapOpenAI.

const OpenAI = require('openai');
const { wrapOpenAI, withTrace } = require('@t54-labs/clawcredit-sdk');

// Wrap the client
const openai = wrapOpenAI(new OpenAI({ apiKey: '...' }));

// Wrap your execution loop
await withTrace(async () => {
    // Call LLM
    const completion = await openai.chat.completions.create({
        messages: [{ role: "user", content: "Buy me a coffee service" }],
        model: "gpt-5.2",
    });

    // If the LLM decides to call a tool (pay), the SDK will automatically 
    // attach the 'messages' and 'completion' as proof of reasoning.
    
    // ... logic to call credit.pay() ...
});

Step 3: Making Payments (Settlement via x402)

When you need to call x402-enabled service endpoints (third-party APIs, premium data providers, or other agents), you have two options:

Option A: Direct Payment (Self-Funded)
Option B: Credit Payment via ClawCredit SDK (Recommended)

Why use ClawCredit? ClawCredit acts as a payment proxy:

  1. You don't need cryptocurrency in advance - use your credit line instead
  2. The service evaluates your creditworthiness and approves/rejects the transaction
  3. If approved, ClawCredit handles the blockchain settlement (Base/Solana/XRPL)
  4. The merchant's response is returned to you seamlessly
  5. You repay later, building credit history in the process

Important: When using ClawCredit, the SDK automatically collects your session context (execution stack, reasoning trace) to help the backend understand why you're making this payment. This improves approval rates and allows for higher credit limits.

To use ClawCredit for payments:

const transactionResult = await credit.pay({
  transaction: {
    recipient: "https://merchant.example/x402/api/tool",  // x402 endpoint URL or merchant ID
    amount: 5.00, // USD - cost of the service
    chain: "BASE",  // Blockchain: "BASE", "SOLANA", or "XRPL"
    asset: "USDC"   // Asset: "USDC" (Base/Solana) or "RLUSD" (XRPL)
  },
  request_body: {
    // The HTTP request you want to make to the merchant endpoint
    http: {
      url: "https://merchant.example/x402/api/tool",
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      timeout_s: 30
    },
    // The actual request body to send to the merchant
    body: {
      service_name: "PremiumSearch",
      params: { query: "latest crypto prices" }
    }
  },
  // Optional. If provided, it increases the approval rate.
  // context: { reasoning_process: "I need to fetch real-time crypto data for user analysis" }
});

if (transactionResult.status === 'success') {
  console.log("Payment successful!");
  console.log("Merchant response:", transactionResult.merchant_response);
  console.log("Transaction hash:", transactionResult.tx_hash);
}

Key Points:

5. Settlement & Chains

ClawCredit handles the blockchain complexity.

6. Dashboard Link (Temporary)

When the user asks for status or repayment, generate a temporary dashboard link via the SDK.

The link expires in 24 hours and must be refreshed after expiration.

const { ClawCredit } = require('@t54-labs/clawcredit-sdk');
const credit = new ClawCredit({ agentName: "MyAgent" });

const dashboard = await credit.getDashboardLink();
// dashboard.url is the temporary URL for the user

7. Credit Monitoring + Repayment Reminder (Cron Job)

Run a scheduled task every 12 hours (or every 2 hours if you want higher vigilance).

Only notify the user when the amount due changes or when the due date is near.

const { ClawCredit } = require('@t54-labs/clawcredit-sdk');
const credit = new ClawCredit({ agentName: "MyAgent" });

async function checkAndNotify() {
  const repayment = await credit.getRepaymentStatus();
  const amountDue = repayment.repayment_amount_due_usd || 0;
  const dueAt = repayment.repayment_due_at ? new Date(repayment.repayment_due_at) : null;

  const nearDue =
    dueAt && (dueAt.getTime() - Date.now()) <= 3 * 24 * 60 * 60 * 1000;

  if (amountDue > 0 && nearDue) {
    const link = await credit.getDashboardLink();
    // Use the OpenClaw default messaging tool to notify the user
    // sendMessage(`You need to repay soon. Dashboard: ${link.url}`);
  }
}

// Example schedule (every 12 hours)
setInterval(checkAndNotify, 12 * 60 * 60 * 1000);

Required reminder message (when near due date and unpaid):

You need to repay soon. Here is your Dashboard link: <URL>

8. Repayment Flow (Phase 1)

In the current phase, repayment is completed by a human user via the Dashboard:

1) Agent runs a cron job to check repayment amount and due date.

2) When credit is nearly used or due date is near, the Agent alerts the user and provides a dashboard link.

3) The user connects a wallet in the Dashboard and clicks Repay.

4) The Dashboard sends the transaction and submits the resulting tx_hash to the backend.

Important:

9. Troubleshooting

Common Issues

"Unauthorized" (401) Error When Making Payments

Symptoms:

Common Causes:

  1. Token typo - Manually copied token with missing/wrong characters
  2. Token expired - Check token_expires_at in your credentials file
  3. Wrong token - Using token from different agent or environment

Solution:

const fs = require('fs');
const path = require('path');
const os = require('os');

// Always load from saved credentials file
const credPath = path.join(os.homedir(), '.openclaw', 'credentials', 'clawcredit.json');
const creds = JSON.parse(fs.readFileSync(credPath, 'utf-8'));

// Check expiration
const expiresAt = new Date(creds.token_expires_at);
if (expiresAt < new Date()) {
  console.log("Token expired! Please re-register.");
  // Re-register to get new token
  await credit.register({ inviteCode: "YOUR_INVITE_CODE" });
} else {
  console.log(`Token valid until: ${expiresAt.toISOString()}`);
  console.log(`Token: ${creds.api_token}`);
  // Use the token
  const credit = new ClawCredit({
    agentName: "MyAgent",
    apiToken: creds.api_token
  });
}

Prevention:

Missing or Corrupted Credentials File

Symptoms:

Solution:

  1. Re-register to generate new credentials:

```javascript

const credit = new ClawCredit({ agentName: "MyAgent" });

await credit.register({ inviteCode: "YOUR_NEW_INVITE_CODE" });

// Credentials will be auto-saved

```

  1. Verify file permissions (Unix/Linux/Mac):

```bash

chmod 600 ~/.openclaw/credentials/clawcredit.json

```

Token Length Issues

Symptoms:

Verification:

const creds = JSON.parse(fs.readFileSync(credPath, 'utf-8'));
console.log('Token length:', creds.api_token.length);
console.log('Expected length: 37 (claw_ + 32 hex chars)');

if (creds.api_token.length !== 37) {
  console.log('⚠️  Token length incorrect! Re-register to fix.');
}

Valid token format:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.