Gravv
/Docs
Search docs or ask a question⌘K
DocsAgent
ChangelogTry sandbox
Recipes Card issuing Close a card and get the money back

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.

4 min read

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.

1.
You read the balance: 435.80 USD.
2.
You block the card. It can no longer spend, so the 435.80 USD stays put.
3.
You read the balance again. If Gravv has already returned it, it shows 0 and you are done.
4.
Otherwise you withdraw 435.80 USD. It lands in the business's card account, or in its linked wallet as 435.80 USDC. The card now holds 0 USD.

Before you start

PrerequisiteHow to check
The card is active, freeze or blocked. An inactive card cannot be withdrawn fromGET /v1/cards/{card_id}
The amount on the cardGET /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

cURLNodePython
curl --request GET \
--url https://api.gravv.xyz/v1/cards/e7b1c2d3-4f5a-4b6c-8d7e-9f0a1b2c3d4e/balance \
--header 'Api-Key: <API_KEY>'

2. Block the card

Block the card so it cannot spend while you take the money off:

cURLNodePython
curl --request PATCH \
--url https://api.gravv.xyz/v1/cards/e7b1c2d3-4f5a-4b6c-8d7e-9f0a1b2c3d4e/update \
--header 'Api-Key: <API_KEY>' \
--header 'Content-Type: application/json' \
--data '
{
"status": "blocked",
"limit": 200
}
'

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.

cURLNodePython
curl --request POST \
--url https://api.gravv.xyz/v1/cards/withdraw \
--header 'Api-Key: <API_KEY>' \
--header 'Idempotency-Key: 7f2155fe-7a1e-4302-83d0-09464cf8bce9' \
--header 'Content-Type: application/json' \
--data '
{
"card_id": "e7b1c2d3-4f5a-4b6c-8d7e-9f0a1b2c3d4e",
"amount": 435.8
}
'

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)WhyFix
cannot withdraw from an inactive cardThe card was never activatedActivate it first
insufficient balanceThe amount is above the card's available balance, often because Gravv already returned it after the blockRead the balance again, and check for cards.transaction.withdraw
amount must be greater than zeroThe amount is 0 or negativeSend a positive amount
a withdrawal for this idempotency key is already in progress; retry shortlyAn earlier call with this key is still runningWait for the webhook or read the balance before you do anything else
internal server error or the request could not be completedThe call failed or timed out. The money may still have movedRead 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.

Reference

Was this page helpful? Yes No