# Developer quick start: Terra Pay and Terra Connect

> Build with Terra Pay and Terra Connect: install the SDK, add a connect button, create a payment and verify signed webhooks. Preview API, subject to change.

Source: https://developers.terrawallet.us/developers

**Status:** The Terra API and SDKs are in early access. Code on this page is a Preview API, subject to change.

## From zero to a verified payment in five minutes

Install the SDK, add a Connect button, create a payment on your server and verify the signed webhook. Connecting to Terra Wallet already works today through open standards.

## Four steps to your first payment

- **1. Get sandbox keys**: Sign in to the dashboard and copy a publishable key (`tw_test_pk_...`) for the browser and a secret key (`tw_test_sk_...`) for your server. Secret keys are shown once.
- **2. Install**: Add the React and server packages. Package names may change before launch.
- **3. Connect and pay**: Wrap your app in `TerraProvider`, drop in `<ConnectButton/>`, and create payments on your server with an idempotency key.
- **4. Verify webhooks**: Check the HMAC-SHA256 signature and timestamp on every event, then read the payment's current state from the API.

Install (Preview API, subject to change):

```bash
# Preview: package names may change before launch.
npm install @terrawallet/react @terrawallet/server
```

Connect (React) (Preview API, subject to change):

```tsx
// Preview API, subject to change. Package names may change.
import { TerraProvider, ConnectButton, useAccount } from "@terrawallet/react";

export function App() {
  return (
    <TerraProvider
      publishableKey="tw_test_pk_..."   // safe in the browser
      theme={{ accent: "#0A5BFF", mode: "auto" }}
    >
      <ConnectButton />
      <Balance />
    </TerraProvider>
  );
}

function Balance() {
  const { address, chain } = useAccount();
  return address ? <p>{chain.name}: {address}</p> : null;
}
```

Create a payment (Preview API, subject to change):

```ts
// Preview API, subject to change. Package names may change.
import { Terra } from "@terrawallet/server";

const terra = new Terra(process.env.TERRA_SECRET_KEY); // tw_test_sk_...

const payment = await terra.payments.create(
  { amount: "120.00", currency: "USD", accept: ["USDC", "USDT"], orderId: "1042" },
  { idempotencyKey: "order-1042" },
);

// Send your customer to payment.checkoutUrl (pay.terrawallet.us/c/...)
```

Subscriptions (Preview API, subject to change):

```ts
// Preview API, subject to change. Package names may change.
const plan = await terra.plans.create({
  name: "Pro",
  amount: "10.00",
  currency: "USDC",
  interval: "month",
  mode: "auto_pay_capped", // or "pay_each_period"
});

const link = await terra.subscriptionLinks.create({ planId: plan.id });
// link.url: share it, or open it with a Subscribe button
```

## Verify every webhook signature

Each event is signed with HMAC-SHA256 over a timestamp and the raw body. Reject old timestamps, compare in constant time, and treat events as hints: the API holds the current state.

- Deliveries may repeat or arrive out of order. Every event has a unique id, so handle each one once.
- Retries with backoff, a delivery log and one-click replay in the dashboard.
- Webhook addresses must be https and public: private, loopback, link-local and metadata addresses are refused on every delivery.

verify.js (Preview API, subject to change):

```js
// Preview API, subject to change: verify a Terra webhook in Node.js.
import { createHmac, timingSafeEqual } from "node:crypto";

// Header example: Terra-Signature: t=1763884800,v1=5f2b...
export function verifyTerraWebhook(rawBody, header, secret) {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  const t = Number(parts.t);
  // Reject anything outside a 5 minute window (replay protection).
  if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false;
  const expected = createHmac("sha256", secret)
    .update(`${t}.${rawBody}`)
    .digest();
  const given = Buffer.from(String(parts.v1 ?? ""), "hex");
  return given.length === expected.length && timingSafeEqual(given, expected);
}

// Then: look up the payment with the API for its current state,
// and treat repeated or out-of-order events as normal.
```

## An API that's predictable for people and AI

- **Versioned and typed**: Versioned `/v1` paths, an OpenAPI 3.1 spec and typed clients generated from it.
- **Helpful errors**: Every error carries a stable `code`, a `doc_url` and a fix hint, plus a request id.
- **Safe retries**: An `Idempotency-Key` on every create, so a retry never charges twice.
- **Strict by default**: Schemas reject unknown fields. Cursor pagination with hard limits, and rate-limit headers on every response.

## Connect to Terra Wallet right now

You don't have to wait for the SDK to support Terra Wallet users: it speaks WalletConnect v2, EIP-1193, EIP-6963 and the Solana Wallet Standard today. See [Terra Connect](/connect) for working code, and [Build with AI](/ai) to work with your assistant.

## Frequently asked questions

### Can I use the API today?

Not yet. The API and SDKs are in early access, and the code here is a Preview API, subject to change. Terra Connect works today through open standards.

### How do I get API keys?

From the dashboard at launch: a publishable key (`tw_test_pk_`) for the browser and a secret key (`tw_test_sk_`) for your server. Secret keys are shown once and stored only as a hash.

### How are webhooks signed?

With HMAC-SHA256 over a timestamp and the raw body. Reject old timestamps, compare in constant time, and read the current state from the API.

### Will retries create duplicate payments?

No. Send an `Idempotency-Key` with every create, and a retry returns the original result.

### Is there a sandbox?

Yes. Test mode runs on testnets, with a simulated payment option and test webhooks.
