Overview
Polymarket’s Builders Program enables developers to integrate prediction markets into their applications with gasless trading and builder order attribution. This guide demonstrates how to use Magic’s passwordless authentication with Polymarket’s CLOB Client and Builder Relayer Client. This guide covers the Safe Wallet implementation using Magic authentication for gasless prediction market trading.Magic Safe Builder Example
Complete Next.js application showing Magic authentication with Safe wallet deployment for Polymarket trading.
Prerequisites
Before starting, ensure you have:- A Magic Publishable API Key from your Magic Dashboard
- Polymarket Builder credentials (Builder ID and signing key)
- A Polygon RPC endpoint
Safe Wallet Architecture
The Safe Wallet flow uses Magic SDK for authentication and the Builder Relayer Client to deploy Gnosis Safe wallets:- User authenticates with Magic (Email OTP or Social Login)
- Magic SDK creates an EOA signer for the user
- Builder Relayer Client deploys a Gnosis Safe wallet
- Token approvals are set via the Relayer
- User API credentials are generated by initializing a temporary ClobClient with the Magic signer and calling
deriveApiKey()orcreateApiKey(). - Authenticated ClobClient is initialized for trading
Placing Orders
With the authenticated ClobClient, you can place market and limit orders.- Market Orders
- Limit Orders
TypeScript
Polymarket’s CLOB doesn’t have true “market orders”. Market orders are simulated using limit orders with aggressive pricing that fills immediately.
- Orders are signed by the user’s EOA (Magic wallet)
- Executed from the Safe wallet address
- Gasless execution - no gas fees for users
- Prompts user signature for each order
Position & Order Management
Fetch User Positions
TypeScript
Fetch Active Orders
TypeScript
Cancel Order
TypeScript
Token Addresses
Important contract addresses on Polygon:TypeScript
Key Dependencies
| Package | Version | Purpose |
|---|---|---|
@polymarket/clob-client | ^4.22.8 | Order placement, User API credentials |
@polymarket/builder-relayer-client | ^0.0.8 | Safe deployment, token approvals |
@polymarket/builder-signing-sdk | ^0.0.8 | Builder configuration |
magic-sdk | latest | Magic authentication |
ethers | ^5.8.0 | Wallet creation, signing |
viem | ^2.39.2 | Ethereum interactions |
Troubleshooting
Safe deployment fails
Safe deployment fails
Symptoms: Cannot deploy Safe walletSolutions:
- Ensure Builder credentials are valid
- Check that the Relayer service is accessible
- Verify the Magic user is authenticated
- Check server-side logs for signing errors
CLOB client not initialized
CLOB client not initialized
Symptoms: Cannot place orders, trading session incompleteSolutions:
- Click “Initialize Trading Session” button
- Ensure Magic authentication is valid
- Check browser console for authentication errors
- Verify User API Credentials were obtained successfully
Balance shows $0.00
Balance shows $0.00
Symptoms: USDC.e balance appears as zeroSolutions:
- Fund the Safe Wallet, not the EOA
- Check Polygonscan for confirmation
- Verify RPC endpoint is working in
.env.local - The wallet address is shown in the header UI
Orders not appearing
Orders not appearing
Symptoms: Orders submitted but not visibleSolutions:
- Wait 2-3 seconds for CLOB sync
- Check USDC.e balance (need funds to trade)
- Verify order was submitted successfully in browser console
- Ensure the wallet is properly funded
Resources
Safe Wallet Demo
Complete implementation with Magic authentication
CLOB Client Docs
Official Polymarket CLOB client documentation
Authentication Guide
Learn about Polymarket’s authentication flow
Order Placement
Guide to placing your first order