npm package: @predexon/trade-sdk
Description: Predexon Trade SDK for Polymarket and Kalshi
Author: Predexon
Latest version: 0.0.7

# Predexon Trade SDK

TypeScript SDK for trading on Polymarket (Polygon) and Kalshi (Solana) prediction markets.

## Installation

```bash
npm install @predexon/trade-sdk
```

Requires Node.js 20.18+.

## Get an API Key

Request access from Predexon to receive an API key.

> **Important:** The SDK is designed for server-side use. Never expose your API key in browser code.

## Quick Start: Polymarket with Viem

Works with any viem `WalletClient` — local accounts, Turnkey, or custom signers. This example shows server-side usage with a local account.

```typescript
import { PredexonSDK, createViemAdapter } from '@predexon/trade-sdk';
import { createWalletClient, http } from 'viem';
import { polygon } from 'viem/chains';
import { privateKeyToAccount } from 'viem/accounts';

// Create a viem wallet client (this example uses a local account)
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const walletClient = createWalletClient({
  account,
  chain: polygon,
  transport: http(process.env.POLYGON_RPC_URL),
});

const wallet = createViemAdapter(walletClient);

const sdk = new PredexonSDK({
  apiKey: process.env.PREDEXON_API_KEY!,
  wallet,
});

// Enable trading (one-time setup)
await sdk.enableTrading({
  core: { venue: 'polymarket', wallet: account.address },
  approvals: 'sign', // Use 'broadcast' if wallet doesn't support signTransaction
});

// Place a market buy order
const order = await sdk.placeOrder({
  core: {
    venue: 'polymarket',
    wallet: account.address,
    side: 'buy',
    amount: '10', // $10 USDC
    type: 'market',
  },
  flags: { tokenId: 'your-token-id' },
});

console.log('Order placed:', order);
```

## Quick Start: Polymarket with Turnkey

```typescript
import { PredexonSDK } from '@predexon/trade-sdk';
import { createTurnkeyAdapter } from '@predexon/trade-sdk/server';

const wallet = createTurnkeyAdapter({
  apiPublicKey: process.env.TURNKEY_API_PUBLIC_KEY!,
  apiPrivateKey: process.env.TURNKEY_API_PRIVATE_KEY!,
  organizationId: process.env.TURNKEY_ORGANIZATION_ID!,
  signWith: process.env.TURNKEY_SIGN_WITH!, // wallet address or wallet ID
  rpcUrl: process.env.POLYGON_RPC_URL,
});

const sdk = new PredexonSDK({
  apiKey: process.env.PREDEXON_API_KEY!,
  wallet,
});

await sdk.enableTrading({
  core: { venue: 'polymarket', wallet: process.env.TURNKEY_SIGN_WITH! },
  approvals: 'sign',
});

const order = await sdk.placeOrder({
  core: {
    venue: 'polymarket',
    wallet: process.env.TURNKEY_SIGN_WITH!,
    side: 'buy',
    amount: '10',
    type: 'market',
  },
  flags: { tokenId: 'your-token-id' },
});
```

Install Turnkey peer dependencies:
```bash
npm install @turnkey/api-key-stamper @turnkey/http @turnkey/viem
```

## Quick Start: Polymarket with Privy

For apps using Privy embedded wallets. Requires server-side Privy credentials and a P256 authorization key configured in the Privy dashboard.

```typescript
import { PredexonSDK } from '@predexon/trade-sdk';
import { createPrivyServerAdapter } from '@predexon/trade-sdk/server';

// userWalletAddress is the Privy embedded wallet address for a specific user
const wallet = createPrivyServerAdapter({
  appId: process.env.PRIVY_APP_ID!,
  appSecret: process.env.PRIVY_APP_SECRET!,
  authorizationPrivateKey: process.env.PRIVY_AUTHORIZATION_PRIVATE_KEY!,
  walletAddress: userWalletAddress, // user's Privy embedded wallet
});

const sdk = new PredexonSDK({
  apiKey: process.env.PREDEXON_API_KEY!,
  wallet,
});

await sdk.enableTrading({
  core: { venue: 'polymarket', wallet: userWalletAddress },
  approvals: 'broadcast', // Privy handles gas internally
});

const order = await sdk.placeOrder({
  core: {
    venue: 'polymarket',
    wallet: userWalletAddress,
    side: 'buy',
    size: '100',
    price: '0.55',
    type: 'limit',
  },
  flags: { tokenId: 'your-token-id' },
});
```

Install Privy peer dependency:
```bash
npm install @privy-io/node
```

## Quick Start: Kalshi

```typescript
import { PredexonSDK, createSolanaAdapter } from '@predexon/trade-sdk';
import { Keypair, VersionedTransaction } from '@solana/web3.js';
import nacl from 'tweetnacl';

const keypair = Keypair.fromSecretKey(/* your secret key bytes */);

const solanaSigner = createSolanaAdapter({
  signSolanaTransaction: async (base64Tx) => {
    const tx = VersionedTransaction.deserialize(Buffer.from(base64Tx, 'base64'));
    tx.sign([keypair]);
    return Buffer.from(tx.serialize()).toString('base64');
  },
  signSolanaMessage: async (message) => {
    const messageBytes = new TextEncoder().encode(message);
    const signature = nacl.sign.detached(messageBytes, keypair.secretKey);
    return Buffer.from(signature).toString('base64');
  },
});

const sdk = new PredexonSDK({
  apiKey: process.env.PREDEXON_API_KEY!,
  solanaSigner,
});

await sdk.enableTrading({
  core: { venue: 'kalshi', wallet: keypair.publicKey.toBase58() },
});

const order = await sdk.placeOrder({
  core: {
    venue: 'kalshi',
    wallet: keypair.publicKey.toBase58(),
    side: 'buy',
    amount: '10',
    type: 'market',
  },
  flags: { ticker: 'MARKET-TICKER', outcome: 'Yes' },
});
```

## Market Identifiers

- Polymarket `tokenId`: https://docs.predexon.com/api-reference/markets/list-markets
- Kalshi `ticker`: https://docs.predexon.com/api-reference/kalshi/list-markets

## Documentation

Full API reference, guides, and examples: **[docs.predexon.com/sdk](https://docs.predexon.com/sdk)**

## Server Adapters

The SDK provides server-side adapters via `@predexon/trade-sdk/server`:

| Adapter | Use Case | Peer Dependencies |
|---------|----------|-------------------|
| `createTurnkeyAdapter` | Turnkey-managed wallets | `@turnkey/api-key-stamper`, `@turnkey/http`, `@turnkey/viem` |
| `createPrivyServerAdapter` | Privy embedded wallets | `@privy-io/node` |
| `createRawKeyAdapter` | **Dev/test only** | None |

## Support

Contact Predexon for access and support.