How to check a bridge's status
This guide shows you how to follow a Fast Bridge request until it finishes. Use it when a response comes back pending, or when you start a request without waiting for it.
Prerequisites
- A Pimlico API key with Fast Bridge enabled.
- The response of a bridge request, from one of the other Fast Bridge guides.
Steps
Add a status function
Create a function that reads a request's status from GET /status/{requestId}.
import type { BridgeStatus } from "./types"
const apiKey = "YOUR_PIMLICO_API_KEY"
// Throws the bridge's { code, message } if it rejected the request
const readResponse = async (response: Response) => {
const body = await response.json()
if (!response.ok) {
throw new Error(`${body.code}: ${body.message}`)
}
return body
}
const getBridgeStatus = async (requestId: string): Promise<BridgeStatus> => {
const response = await fetch(
`https://api.pimlico.io/v1/bridge/status/${requestId}?apikey=${apiKey}`,
)
return readResponse(response)
}export type BridgeStatus = {
requestId: string
status: "pending" | "success" | "failed" | "refunded"
amountIn: string
fee: string
amountOut: string
origin: { chainId: number; status: string; transactionHash?: string }
destination: {
chainId: number
recipient: string
status: string
transactionHash?: string
error?: { code: string; message: string }
}
}Poll until the request finishes
Poll the request every second until it's no longer pending, or until its delivery has failed.
// Polls until the request is no longer pending, or its delivery has failed
const waitForBridge = async (pending: BridgeStatus): Promise<BridgeStatus> => {
let status = pending
while (status.status === "pending" && !status.destination.error) {
await new Promise((resolve) => setTimeout(resolve, 1_000))
status = await getBridgeStatus(status.requestId)
}
return status
}
const final = await waitForBridge(result)Check the outcome
Read status from the result:
success: the USDC was delivered indestination.transactionHash.failed: nothing was bridged, and the USDC is still on the origin chain.pending: the delivery failed, anddestination.errorsays why. See leg errors.
if (final.status === "success") {
console.log("Delivered:", final.destination.transactionHash)
} else if (final.status === "failed") {
console.log("Nothing was bridged")
} else {
console.log("Delivery failed:", final.destination.error)
}Start a request without waiting
To get a response as soon as the bridge accepts a request, send it to POST /execute instead of POST /execute/sync, then poll it.
// Starts the request and returns as soon as the bridge has accepted it
const startBridge = async (body: object): Promise<BridgeStatus> => {
const response = await fetch(
`https://api.pimlico.io/v1/bridge/execute?apikey=${apiKey}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
},
)
return readResponse(response)
}
const started = await startBridge(request)
const outcome = await waitForBridge(started)