How to authenticate your API requests
Authentication with API Key and JWT Token
To interact with Predict's API, you'll need two things:
- API Key: Required for all endpoints (only on Mainnet).
- JWT Token: Required for performing personal operations for a specific wallet (e.g., sending a new order or viewing active orders).
Sections:
- Obtaining a JWT Token (for EOAs)
- Obtaining a JWT Token (for Predict accounts)
- Passing the API Key and JWT Token in the requests
Obtaining a JWT Token (for EOAs)
An API key is required to obtain a JWT token. Follow these steps to generate a JWT token for your wallet:
Retrieve the message to sign:
Send aGET
request to/auth/message
to retrieve a message for signing.Sign the message with your wallet:
Use the wallet you want to authenticate with to sign the message retrieved in step 1.Send the signature:
Send aPOST
request to/auth
with the following JSON structure:
import { Wallet } from "ethers";
// Create a wallet to sign the message (must be the orders' `maker`)
const signer = new Wallet(process.env.WALLET_PRIVATE_KEY);
async function main() {
// Send the `GET auth/message` request
const messageRequest = await fetch("https://api.predict.fun/auth/message", {
method: "GET",
headers: {
"x-api-key": "YOUR_API_KEY",
},
});
// Await for the response
const messageResponse = await messageRequest.json();
// Retrive the message to sign
const message = messageResponse.data.message;
// Sign the message
const signature = await signer.signMessage(message);
// The body's data to request the JWT via `POST auth`
const body = {
signer: signer.address,
message: message,
signature: signature,
};
// Send the `POST auth` request
const jwtRequest = await fetch("https://api.predict.fun/auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": "YOUR_API_KEY",
},
body: JSON.stringify(body),
});
// Await for the response
const jwtResponse = await jwtRequest.json();
// Fetch the JWT token
const jwt = jwtResponse.data.token;
}
Obtaining a JWT Token (for Predict accounts)
An API key is required to obtain a JWT token. Follow these steps to generate a JWT token for your wallet:
Retrieve the message to sign:
Send aGET
request to/auth/message
to retrieve a message for signing.Sign the message with your wallet via our SDK:
You will need two wallets:- Your Predict account address (aka deposit address)
- Your Privy Wallet private key (can be exported from the account's settings)
NOTE: It's reccomended to fund with ETH your Privy Wallet to be able to set approvals and cancel orders.
Send the signature:
Send aPOST
request to/auth
with the following JSON structure:
import { Wallet } from "ethers";
// You can export this private key from your account's settings at https://predict.fun/account/settings
const signer = new Wallet(process.env.PRIVY_WALLET_PRIVATE_KEY);
async function main() {
/**
* NOTE: Replace `PREDICT_ACCOUNT_ADDRESS` with your Predict account address/deposit address.
*/
// Create a new instance of the OrderBuilder class. Note: This should only be done once per signer
const builder = await OrderBuilder.make(ChainId.BlastMainnet, privyWallet, {
predictAccount: "PREDICT_ACCOUNT_ADDRESS",
});
// Call an helper function to get the jwt token and provide the OrderBuilder instance
// Only needed once per session
await getAuthJWT(builder);
}
async function getAuthJWT(builder) {
// Send the `GET auth/message` request
const messageRequest = await fetch("https://api.predict.fun/auth/message", {
method: "GET",
headers: {
"x-api-key": "YOUR_API_KEY",
},
});
// Await for the response
const messageResponse = await messageRequest.json();
// Retrive the message to sign
const message = messageResponse.data.message;
// Sign the message using the SDK function for Predict accounts
// The standard `signMessage` won't work
const signature = await builder.signPredictAccountMessage(message);
// The body's data to request the JWT via `POST auth`
const body = {
signer: "PREDICT_ACCOUNT_ADDRESS",
message: message,
signature: signature,
};
// Send the `POST auth` request
const jwtRequest = await fetch("https://api.predict.fun/auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": "YOUR_API_KEY",
},
body: JSON.stringify(body),
});
// Await for the response
const jwtResponse = await jwtRequest.json();
// Fetch the JWT token
const jwt = jwtResponse.data.token;
return jwt;
}
Passing the API Key and JWT Token in Requests
To authenticate your requests, you need to include both the API key and the JWT token in the request headers. The API key and x-api-key
header are not required on Sepolia.
Request headers (as shown on the example above):
{
"headers": {
"x-api-key": "YOUR_API_KEY",
"Authorization": "Bearer YOUR_JWT_TOKEN"
}
}
Example usage:
```typescript
async function main() {
// Example on how to send API requests with both
const apiRequest = await fetch("API_ENDPOINT_HERE", {
method: "POST", // Or "GET"
headers: {
"Content-Type": "application/json",
"x-api-key": "YOUR_API_KEY",
Authorization: Bearer ${jwt}
,
},
body: JSON.stringify(someData),
});
const apiResponse = await apiRequest.json();
}