Transfer Balance

In this code snippet you will see how to transfer part (or all) of the parent account's balance to a subaccount.

Example

Ensure the following variables are set to your required values using any convenient method:

Key Description
VONAGE_API_KEY The API key of the parent account.
VONAGE_API_SECRET The API secret of the parent account.
SUBACCOUNT_KEY The API key of the subaccount to receive the specified amount.
AMOUNT The amount of balance to be transferred to the specified subaccount.

Write the code

Add the following to transfer-balance.sh:

curl -X "POST" "https://api.nexmo.com/accounts/$VONAGE_API_KEY/balance-transfers" -u $VONAGE_API_KEY:$VONAGE_API_SECRET \
     -H "Content-Type: application/json"  \
     -d $'{"from":"'$VONAGE_API_KEY'", "to":"'$SUBACCOUNT_KEY'", "amount": '$AMOUNT'}'

View full source

Run your code

Save this file to your machine and run it:

bash transfer-balance.sh

Prerequisites

npm install @vonage/subaccounts

Create a file named transfer-balance.js and add the following code:

const { SubAccounts } = require('@vonage/subaccounts');

const subAccountClient = new SubAccounts({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET,
});

View full source

Write the code

Add the following to transfer-balance.js:

subAccountClient.transferBalance({
  from: VONAGE_API_KEY,
  to: SUBACCOUNT_KEY,
  amount: AMOUNT,
})
  .then((balanceTransfer) => console.log(balanceTransfer))
  .catch((error) => console.error(error));

View full source

Run your code

Save this file to your machine and run it:

node transfer-balance.js

Prerequisites

Add the following to build.gradle:

implementation 'com.vonage:server-sdk-kotlin:2.1.1'

Create a file named TransferBalance and add the following code to the main method:

val client = Vonage {
    apiKey(VONAGE_API_KEY)
    apiSecret(VONAGE_API_SECRET)
}

View full source

Write the code

Add the following to the main method of the TransferBalance file:

val receipt = client.subaccounts.transferBalance(
    from = VONAGE_API_KEY,
    to = SUBACCOUNT_KEY,
    amount = SUBACCOUNT_BALANCE_AMOUNT
)

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.subaccounts with the package containing TransferBalance:

gradle run -Pmain=com.vonage.quickstart.kt.subaccounts.TransferBalance

Prerequisites

Add the following to build.gradle:

implementation 'com.vonage:server-sdk:9.3.1'

Create a file named TransferBalance and add the following code to the main method:

VonageClient client = VonageClient.builder()
		.apiKey(VONAGE_API_KEY)
		.apiSecret(VONAGE_API_SECRET)
		.build();

View full source

Write the code

Add the following to the main method of the TransferBalance file:

MoneyTransfer receipt = client.getSubaccountsClient().transferBalance(
		MoneyTransfer.builder()
			.from(VONAGE_API_KEY).to(SUBACCOUNT_KEY)
			.amount(SUBACCOUNT_BALANCE_AMOUNT).build()
);
System.out.println("Transfer successful: "+receipt.getId());

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.subaccounts with the package containing TransferBalance:

gradle run -Pmain=com.vonage.quickstart.subaccounts.TransferBalance

Prerequisites

Install-Package Vonage

Create a file named TransferBalanceRequest.cs and add the following code:

using Vonage;
using Vonage.Request;
using Vonage.SubAccounts.TransferAmount;

View full source

Add the following to TransferBalanceRequest.cs:

var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(credentials);

View full source

Write the code

Add the following to TransferBalanceRequest.cs:

var request = TransferAmountRequest.Build()
    .WithFrom(VONAGE_API_KEY)
    .WithTo(SUBACCOUNT_KEY)
    .WithAmount(amount)
    .Create();
var response = await client.SubAccountsClient.TransferBalanceAsync(request);

View full source

Prerequisites

composer require vonage/client

Create a file named transfer-balance.php and add the following code:

$basic = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);

View full source

Write the code

Add the following to transfer-balance.php:

$transferRequest = new \Vonage\Subaccount\Request\TransferBalanceRequest(VONAGE_API_KEY);
$transferRequest
    ->setFrom(VONAGE_API_KEY)
    ->setTo(SUBACCOUNT_KEY)
    ->setAmount(250);

$subaccount = $client->subaccount()->makeBalanceTransfer($transferRequest);

View full source

Run your code

Save this file to your machine and run it:

php transfer-balance.php

Prerequisites

pip install vonage python-dotenv

Write the code

Add the following to transfer-balance.py:

from vonage import Auth, Vonage
from vonage_subaccounts import Transfer, TransferRequest

client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))

request = TransferRequest(
    from_=VONAGE_API_KEY, to=SUBACCOUNT_KEY, amount=SUBACCOUNT_BALANCE_AMOUNT
)

transfer: Transfer = client.subaccounts.transfer_balance(request)

print(transfer)

View full source

Run your code

Save this file to your machine and run it:

python subaccounts/transfer-balance.py

Prerequisites

gem install vonage

Create a file named transfer-balance.rb and add the following code:

client = Vonage::Client.new(
  api_key: VONAGE_API_KEY,
  api_secret: VONAGE_API_SECRET
)

View full source

Write the code

Add the following to transfer-balance.rb:

client.subaccounts.transfer_balance(
  from: VONAGE_API_KEY,
  to: SUBACCOUNT_KEY,
  amount: AMOUNT
)

View full source

Run your code

Save this file to your machine and run it:

ruby transfer-balance.rb

Try it out

When you run the code you will transfer the specified amount of the parent account's balance to the specified subaccount.