Create Token Transactions
Introduction
In this tutorial, we will cover how to create ERC20 (Ethereum), BEP20 (Binance Smart Chain), and TRC20 (Tron) token transactions using the Chaingateway V2 API. We will provide examples in various programming languages, including Shell (cURL), PHP, Python, and JavaScript.
For obtaining an API key and learning about authorization, please refer to the Getting Started section of the ChainGateway documentation.
To make transactions from Ethereum, BSC or Polygon, your address needs to be imported to chaingateway or was created with the chaingateway api. Otherwise the authorization procedure will not work.
To create an address, refer to tutorial section
Creating ERC20 and BEP20 Transactions
When making a token transaction on Ethereum, Polygon (ERC20) or Binance Smart Chain (BEP20), you need to send a request to the Chaingateway API with the appropriate parameters.
Usage for other Blockhains and networks
This code example is using /ethereum
in the route as an example. feel free to just replace it with /bsc
, /polygon
or /bitcoin
for creating it on other networks.
You can also replace https://api.chaingateway.io
with https://testnet.chaingateway.io
to switch to the testnet networks.
As the token type is also different, we will provide the paths here:
Blockchain | API Route |
---|---|
Ethereum | /v2/ethereum/transactions/erc20 |
Binance Smart Chain | /v2/bsc/transactions/bep20 |
Tron | /v2/tron/transactions/trc20 |
Polygon | /v2/polygon/transactions/erc20 |
Parameters
- from: The sender's address.
- to: The recipient's address.
- amount: The amount to send.
- contractaddress: The contract address of the token.
- password: The password used to encrypt the keystore file of the sender's address.
Example Authorization Header
Replace YOUR_API_TOKEN
with your actual API token.
Example Code
curl --request POST\
--url https://api.chaingateway.io/v2/ethereum/transactions/erc20\
--header 'Accept: application/json'\
--header 'content-type: application/json'\
--header 'Authorization: YOUR_API_TOKEN'\
--data '{
"from": "0xYourSenderAddress",
"to": "0xRecipientAddress",
"amount": "100",
"contractaddress": "0xTokenContractAddress",
"password": "YourPassword"
}'
import requests
url = "https://api.chaingateway.io/v2/ethereum/transactions/erc20"
payload = {
"from": "0xYourSenderAddress",
"to": "0xRecipientAddress",
"amount": "100",
"contractaddress": "0xTokenContractAddress",
"password": "YourPassword"
}
headers = {
'Accept': 'application/json',
'content-type': 'application/json',
'Authorization': 'YOUR_API_TOKEN'
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
const axios = require('axios');
const url = 'https://api.chaingateway.io/v2/ethereum/transactions/erc20';
const payload = {
from: '0xYourSenderAddress',
to: '0xRecipientAddress',
amount: '100',
contractaddress: '0xTokenContractAddress',
password: 'YourPassword'
};
const headers = {
'Accept': 'application/json',
'content-type': 'application/json',
'Authorization': 'YOUR_API_TOKEN'
};
axios.post(url, payload, { headers })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('https://api.chaingateway.io/v2/ethereum/transactions/erc20', [
'headers' => [
'Accept' => 'application/json',
'content-type' => 'application/json',
'Authorization' => 'YOUR_API_TOKEN',
],
'json' => [
'from' => '0xYourSenderAddress',
'to' => '0xRecipientAddress',
'amount' => '100',
'contractaddress' => '0xTokenContractAddress',
'password' => 'YourPassword'
]
]);
echo $response->getBody();
?>
The code should return this response:
{
"status": 201,
"ok": true,
"message": "Succesfully created transaction",
"data": {
"txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27"
}
}
Creating TRC20 Transactions
Creating Transactions for Tron Tokens works nearly the same. The only difference is that you will need to provide the privateKey
intead of the password
parameter.
Parameters
- from: The sender's address.
- to: The recipient's address.
- amount: The amount to send.
- contractaddress: The contract address of the token.
- privateKey: The private key of the sender's address.
Example Authorization Header
Replace YOUR_API_TOKEN
with your actual API token.
Example Code
curl --request POST\
--url https://api.chaingateway.io/v2/tron/transactions/trc20\
--header 'Accept: application/json'\
--header 'content-type: application/json'\
--header 'Authorization: YOUR_API_TOKEN'\
--data '{
"from": "TYourSenderAddress",
"to": "TRecipientAddress",
"amount": "100",
"contractaddress": "TTokenContractAddress",
"privateKey": "YourPrivateKey"
}'
import requests
url = "https://api.chaingateway.io/v2/tron/transactions/trc20"
payload = {
"from": "TYourSenderAddress",
"to": "TRecipientAddress",
"amount": "100",
"contractaddress": "TTokenContractAddress",
"privateKey": "YourPrivateKey"
}
headers = {
'Accept': 'application/json',
'content-type': 'application/json',
'Authorization': 'YOUR_API_TOKEN'
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
import requests
url = "https://api.chaingateway.io/v2/tron/transactions/trc20"
payload = {
"from": "TYourSenderAddress",
"to": "TRecipientAddress",
"amount": "100",
"contractaddress": "TTokenContractAddress",
"privateKey": "YourPrivateKey"
}
headers = {
'Accept': 'application/json',
'content-type': 'application/json',
'Authorization': 'YOUR_API_TOKEN'
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('https://api.chaingateway.io/v2/tron/transactions/trc20', [
'headers' => [
'Accept' => 'application/json',
'content-type' => 'application/json',
'Authorization' => 'YOUR_API_TOKEN',
],
'json' => [
'from' => 'TYourSenderAddress',
'to' => 'TRecipientAddress',
'amount' => '100',
'contractaddress' => 'TTokenContractAddress',
'privateKey' => 'YourPrivateKey'
]
]);
echo $response->getBody();
?>
The code should return this response:
{
"status": 201,
"ok": true,
"message": "Succesfully created transaction",
"data": {
"txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27"
}
}