> For the complete documentation index, see [llms.txt](https://docs.scrt.network/secret-network-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.scrt.network/secret-network-documentation/secretvm-confidential-virtual-machines/agentic-support/confidential-llm-access-for-agents-x402/complete-sample-agent.md).

# Complete Sample Agent

A minimal self-contained agent client in Node.js (requires `ethers` v6):

```javascript
import { ethers } from 'ethers';
import { createHash } from 'crypto';

class X402Agent {
  constructor(baseURL, privateKey) {
    this.baseURL = baseURL.replace(/\/$/, '');
    this.wallet = new ethers.Wallet(privateKey);
  }

  async signedFetch(method, path, body = '') {
    const timestamp = String(Math.floor(Date.now() / 1000));
    const payload = method + path + body + timestamp;
    const hashBytes = createHash('sha256').update(payload).digest();
    const signature = await this.wallet.signMessage(hashBytes);

    const res = await fetch(this.baseURL + path, {
      method,
      headers: {
        'Content-Type': 'application/json',
        'X-Agent-Address': this.wallet.address,
        'X-Agent-Signature': signature,
        'X-Agent-Timestamp': timestamp,
      },
      body: body || undefined,
    });
    // 200 and 402 bodies are JSON; 401/503 errors are plain text
    const text = await res.text();
    let json = null;
    try { json = JSON.parse(text); } catch {}
    return { status: res.status, body: json ?? text };
  }

  chat(model, message) {
    const body = JSON.stringify({
      model,
      messages: [{ role: 'user', content: message }],
      stream: false,
    });
    return this.signedFetch('POST', '/v1/chat/completions', body);
  }

  models() {
    return this.signedFetch('GET', '/v1/models');
  }
}

// Usage
const agent = new X402Agent('https://<secretai-llm-endpoint>', process.env.AGENT_PRIVATE_KEY);

const { status, body } = await agent.chat('gpt-oss:120b', 'Hello from my agent!');
if (status === 200) {
  console.log(body.choices[0].message.content);
} else if (status === 402) {
  console.log(`Top up needed: ${body.topup_amount_usd} USD at ${body.topup_url}`);
} else {
  console.error(`HTTP ${status}: ${typeof body === 'string' ? body : JSON.stringify(body)}`);
}
```

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.scrt.network/secret-network-documentation/secretvm-confidential-virtual-machines/agentic-support/confidential-llm-access-for-agents-x402/complete-sample-agent.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
