Skip to content

Commit 1b84ead

Browse files
committed
add errors
1 parent 5048733 commit 1b84ead

1 file changed

Lines changed: 87 additions & 1 deletion

File tree

fern/docs/pages/0x-swap-api/additional-topics/swap-and-send.mdx

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Swap and Send"
33
description: "Send swapped tokens directly to any address using the recipient parameter."
44
---
55

6-
By default, swapped tokens are returned to the address that initiates the trade (the taker). With the `recipient` parameter, you can send the output tokens directly to any wallet address — in a single transaction.
6+
By default, swapped tokens are returned to the address that initiates the trade (the `taker`). With the `recipient` parameter, you can send the output tokens directly to any wallet address — in a single transaction.
77

88
This unlocks use cases like:
99

@@ -195,6 +195,92 @@ recipient=0xRecipientAddress" \
195195

196196
---
197197

198+
## Errors
199+
200+
The API validates recipient at the price and quote stage — before any transaction is submitted — so no gas is wasted. There are two common error cases: an [invalid recipient address](/docs/0x-swap-api/additional-topics/swap-and-send#invalid-recipient-address) and passing recipient on a [wrap/unwrap operation](/docs/0x-swap-api/additional-topics/swap-and-send#wrapunwrap-operations).
201+
202+
### Invalid recipient address
203+
204+
The `recipient` value must be a valid Ethereum address — a 42-character hex string starting with `0x`. This error is triggered by things like a truncated address, a non-hex character, or passing a raw ENS name instead of a resolved address.
205+
206+
<Callout type="info">
207+
Always resolve ENS names to addresses client-side before passing them to the API. The `recipient` field does not support ENS.
208+
</Callout>
209+
210+
```json
211+
{
212+
"name": "INPUT_INVALID",
213+
"message": "The input is invalid",
214+
"data": {
215+
"zid": "0x8843f11733fbf965f90ef10a",
216+
"details": [
217+
{
218+
"field": "recipient",
219+
"reason": "Invalid ethereum address"
220+
}
221+
]
222+
}
223+
}
224+
```
225+
226+
To catch this before hitting the API, validate the address client-side:
227+
228+
```typescript
229+
import { isAddress, getAddress } from "viem";
230+
231+
const raw = userInputAddress;
232+
233+
if (!isAddress(raw)) {
234+
throw new Error("Invalid recipient address");
235+
}
236+
237+
const recipient = getAddress(raw); // normalizes to EIP-55 checksum
238+
```
239+
240+
### Wrap/unwrap operations
241+
242+
`recipient` is not supported when `sellToken` and `buyToken` are a native/wrapped pair (e.g. ETH ↔ WETH, MATIC ↔ WMATIC). These operations don't route through the swap protocol, so the `recipient` field has no effect and is rejected.
243+
244+
```json
245+
{
246+
"name": "RECIPIENT_NOT_SUPPORTED",
247+
"message": "The recipient parameter is not supported for wrap/unwrap operations",
248+
"data": {
249+
"zid": "0x05e4e8147d97597183b471ca"
250+
}
251+
}
252+
```
253+
254+
Guard against this by detecting wrap/unwrap pairs before building your request and omitting `recipient` in those cases:
255+
256+
```typescript
257+
const WRAP_UNWRAP_PAIRS: [string, string][] = [
258+
["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"], // ETH ↔ WETH
259+
// add other pairs as needed
260+
];
261+
262+
const isWrapUnwrap = (sellToken: string, buyToken: string) =>
263+
WRAP_UNWRAP_PAIRS.some(
264+
([a, b]) =>
265+
(sellToken.toLowerCase() === a.toLowerCase() && buyToken.toLowerCase() === b.toLowerCase()) ||
266+
(sellToken.toLowerCase() === b.toLowerCase() && buyToken.toLowerCase() === a.toLowerCase())
267+
);
268+
269+
const params = {
270+
sellToken,
271+
buyToken,
272+
sellAmount,
273+
taker,
274+
...(!isWrapUnwrap(sellToken, buyToken) && { recipient }),
275+
};
276+
```
277+
278+
The `zid` in the error response is a trace ID — include it when contacting support to help diagnose the issue.
279+
280+
---
281+
282+
283+
198284
## Key considerations
199285

200286
- **Taker still pays.** The `taker` signs and funds the transaction. Only the output destination changes.

0 commit comments

Comments
 (0)