> ## Documentation Index
> Fetch the complete documentation index at: https://docs.molpha.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Quickstart: Backend

> Use the TypeScript SDK to request signed data, verify it, and optionally publish it to Solana.

Backends use Molpha when they need a verifiable API result that can later be checked by a smart contract, another service, or an AI agent.

`sdk.gateway.requestSignedData` calls the subscription-backed gateway route (`POST /v1/round/execute`). A fresh wallet that has not subscribed (or been registered as a [Delegate](/access-models/delegates)) receives `403` instead of a payload. Subscribe once with the signing wallet before the first round — or use the [x402 agent path](/access-models/x402) / [AI agents quickstart](/quickstarts/ai-agents) for pay-per-request access.

## 1. Install the SDK

```bash theme={null}
npm install @molpha/sdk @anchor-lang/core bn.js
```

Requires Node.js `>=20.19.0`.

## 2. Initialize

```ts theme={null}
import { web3 } from "@anchor-lang/core";
import {
  MolphaSDK,
  PlanType,
  deriveApiConfigHash,
  deriveFeedIdString,
} from "@molpha/sdk";
import { walletFromKeypairFile } from "@molpha/sdk/utils";

const wallet = walletFromKeypairFile("~/.config/solana/id.json");
const sdk = new MolphaSDK({
  connection: new web3.Connection("https://api.devnet.solana.com", "confirmed"),
  wallet,
});
```

## 3. Subscribe

Gateway rounds settle against an active USDC plan. Skip this only if the wallet already has a live subscription (or is an active delegate under one).

```ts theme={null}
const plan = await sdk.solana.getPlan(PlanType.Basic);
await sdk.solana.subscribe(PlanType.Basic, {
  maxPriceUsdc: plan.subscriptionPrice,
});
```

`maxPriceUsdc` is a safety bound: the transaction aborts if the on-chain price exceeds what you approved. Needs Devnet SOL for fees and Devnet USDC for the plan.

If a separate hot key will sign gateway auth instead of the subscription owner, register it with [`add_delegate`](/access-models/delegates) before calling `requestSignedData`.

## 4. Derive a feed ID

```ts theme={null}
const apiConfig = {
  url: "https://api.example.com/price",
  responseParser: "$.price",
};

const signaturesRequired = 3;
const feedId = deriveFeedIdString(
  wallet.publicKey.toBytes(),
  deriveApiConfigHash(apiConfig),
  signaturesRequired,
);
```

## 5. Request a signed payload

```ts theme={null}
const result = await sdk.gateway.requestSignedData({
  feedId,
  apiConfig,
  signaturesRequired,
  maxAge: 60,
});

console.log(result.value, result.timestamp, result.signersBitmap);
```

The result is ready for Solana submission, EVM verification, Starknet verification, or off-chain audit logging.

## 6. Publish to Solana when you need a maintained feed

```ts theme={null}
const { signature } = await sdk.solana.submitDataUpdate(result);
console.log("submitted", signature);
```

Reading the maintained feed later:

```ts theme={null}
const feed = await sdk.solana.readFeed(feedId);
console.log(feed?.value, feed?.canonicalTimestamp); // value is on-chain bytes
```

The first successful submit creates the Solana `Feed` account if it does not exist.

## 7. Build contract arguments

```ts theme={null}
import { buildEvmVerifierArgs, buildStarknetVerifierArgs } from "@molpha/sdk";

const evmArgs = buildEvmVerifierArgs(result);
const starknetArgs = buildStarknetVerifierArgs(result);
```

## Backend patterns

| Pattern           | Use it when                                                     |
| ----------------- | --------------------------------------------------------------- |
| Request only      | You need a portable proof artifact for another service or chain |
| Request + submit  | You maintain a Solana feed that many consumers read             |
| Request + verify  | You gate an off-chain decision on a threshold-signed API result |
| Request + handoff | You pass verified data to an agent or contract executor         |

## Next steps

<CardGroup cols={2}>
  <Card title="SDK overview" icon="code" href="/sdk/overview">
    Installation, wallet interface, and facade methods.
  </Card>

  <Card title="Gateway API" icon="cloud" href="/sdk/gateway-api">
    Request shape, response fields, caching, and auth notes.
  </Card>

  <Card title="Subscriptions" icon="credit-card" href="/access-models/subscriptions">
    Plans, quota, and renewing access for `/v1/round/execute`.
  </Card>

  <Card title="x402 escrow" icon="wallet" href="/access-models/x402">
    Pay-per-request agent path when you do not want a monthly plan.
  </Card>
</CardGroup>
