Close a card and get the money back
Block a card so it cannot spend, then withdraw its balance to its linked card account or linked wallet.
Use this recipe to close a card and take the money off it. You block the card first, so its balance cannot change, then withdraw everything to the card's linked account or wallet.
Who it is for. A consumer app whose user closes their account, or an expense platform whose employee leaves.
Money example
Tunde's card holds 435.80 USD.
Before you start
| Prerequisite | How to check |
|---|---|
The card is active, freeze or blocked. An inactive card cannot be withdrawn from | GET /v1/cards/{card_id} |
| The amount on the card | GET /v1/cards/{card_id}/balance |
Shared balances. If shared is true, the balance belongs to several cards, and withdrawing takes money the other cards can spend. Decide what the other cards keep before you withdraw.
Steps
1. Read the balance
2. Block the card
Block the card so it cannot spend while you take the money off:
3. Read the balance again, then withdraw what is left
After a block, Gravv may return the balance to the card's linked account or wallet by itself. It does this only when every card sharing the balance is blocked (one frozen card keeps it on the cards) and the amount is at least 1 USD. That return is an ordinary card withdrawal: you receive cards.transaction.withdraw with the description Balance returned after card block, and the money lands where a withdrawal would.
So read the balance again first, and withdraw only what is still available. If money is still available a minute after the block, withdraw it yourself, as below. If you withdraw while the return is running, one of the two can answer insufficient balance, or answer pending and then fail; the money is not lost, it has gone back already.
amount is a JSON number in dollars with at most 2 decimals. The money goes to the card's linked account or wallet. The answer has status completed (paid out straight away, with tx_hash) or pending (paid out by transfer, with transfer_id). Either way you then receive cards.transaction.withdraw. See Withdraw from card for both answers.
What can go wrong
| Response (HTTP 400) | Why | Fix |
|---|---|---|
cannot withdraw from an inactive card | The card was never activated | Activate it first |
insufficient balance | The amount is above the card's available balance, often because Gravv already returned it after the block | Read the balance again, and check for cards.transaction.withdraw |
amount must be greater than zero | The amount is 0 or negative | Send a positive amount |
a withdrawal for this idempotency key is already in progress; retry shortly | An earlier call with this key is still running | Wait for the webhook or read the balance before you do anything else |
internal server error or the request could not be completed | The call failed or timed out. The money may still have moved | Read the balance and your webhooks before you retry. If the money did not move, retry with a new key |
Webhooks you will receive
When you block the card, you receive cards.status.update with blocked. When a withdrawal ends, yours or the automatic return after the block, you receive cards.transaction.withdraw with transaction.status and transaction.amount.
Testing in sandbox
Withdrawals work on sandbox cards. A sandbox card linked to a wallet pays out sandbox test dollars to that wallet.