Balance Inquiry Using JSON
Overview
This method provides a way to send check for the EgoSms balance using JSON.
Check balance by making an HTTP POSTĀ request to the following endpoints:
LIVE URL: https://www.egosms.co/api/v1/json/
SANDBOX URL: http://sandbox.egosms.co/api/v1/json/
JSON Balance Request Parameters
These are the standard request parameters that are needed when using the EgoSms Balance Inquiry JSON API.
methodString Required
Specifies the action being performed. ie Balance for Balance Inquiry.
usernameString Required
The account username.
passwordString Required
The account password.
Example
Submitted JSON
{
"method":"Balance",
"userdata":{
"username":"Egotest",
"password":"xxxxxxxxxxx"
}
}
System Feedback
A Successful Request
Once the request has been successfully submitted, it will return the balance of the account in Json format as shown below.
{
"Status":"OK",
"Balance":"1380"
}
A Failed Request
Once the request fails, JSONĀ in theĀ format below will be returned.
{
"Status":"Failed",
"Message":"xxxxxxx"
}
Possible Error Messages
Error
Meaning
Wrong Username or Password.
Occurs if either your username or password is wrong.
That user does not exist or user not active
Occurs if the account being used does not exist or isĀ inactive or if the username is not set.
One Of The Values Is Empty
Occurs if any of the parameters is empty.
Method Of Request Not Set
Occurs if the method value is empty.
Programming Example
'Balance',
'userdata' => array(
'username' => 'Egotest', // Egosms Username
'password' => 'xxxxxxxxxxx'
)
);
//encode the array into json
$json_builder = json_encode($data);
//use curl to post the the json encoded information
$ch = curl_init('http://www.egosms.co/api/v1/json/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_builder);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
curl_close($ch);
//print an array that is json decoded
print_r(json_decode($ch_result, true));
?>
# importing the requests library
import json
import requests
import html
# The api url
url = "https://www.egosms.co/api/v1/json/"
# The parameters to be sent to the ego sms api
password = "xxxxxx"
username = "egotest"
# data to be sent to api
data = {
"method": "Balance",
"userdata": {
"username": html.escape(username),
"password": html.escape(password)
}
}
data_in_json = json.dumps(data)
timeout = 5
# Check for an internet connection and make the request
try:
# sending post request and saving response as response object
r = requests.post(url=url, data=data_in_json)
# extracting response text
response = r.text
print(response)
except(requests.ConnectionError, requests.Timeout) as exception:
print("Check your internet connection")
/**
* Java API for Egosms Balance inquiry using JSON
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
public class CheckBalanceJSON {
public static void main(String args[]) {
try {
/**
* Egosms url for Balance inquiry using JSON
*/
URL url = new URL("https://www.egosms.co/api/v1/json/");
/**
* Opening the connection to the egosms JSON url
*/
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
/**
* Setting the request method to POST
*/
http.setRequestMethod("POST");
http.setDoOutput(true);
/**
* User name and password for a registered Egosms user
*/
String username = "Egosmstest";
String password = "egotest";
/**
* Passing the method(Balance), username and password in JSON format The method,
* username and password are appended to the url as parameters when sending the
* data
*/
byte[] out = ("{\"method\":\"Balance\"," + "\"userdata\":" + "{" + "\"username\":\"" + username + "\","
+ "\"password\":\"" + password + "\"" + "}" + "}").getBytes(StandardCharsets.UTF_8);
int length = out.length;
//For Testing purposes
String outString = new String(out);
System.out.println("Content to send : " + outString);
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
http.connect();
try (OutputStream os = http.getOutputStream()) {
os.write(out);
}
/**
* Setting the input stream to read the response data
*/
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
String line = null;
/**
* Printing the response data
*/
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}