File Messages
To ensure successful communication with the API, please include the following headers in all your requests:
Content-Type
: application/jsonAccept
: application/json
In addition to the previous process, there are few parameters that may change on the request body; startTime
,
startDate
, endDate
, and scheduledOption
.
To ensure successful communication with the API, please include the following headers in all your requests:
The API responses will be returned in the following format:
{
"success": true,
"message": "Message sent successfully",
"data": { }
}
- Javascript
- PHP
- Java
const sendFileMessage = async() => {
const token = 'YOUR_API_TOKEN'
const selectedFile = document.getElementById('file').files[0]
const baseUrl = 'https://example.com/v2'
try {
const formData = new FormData()
formData.append('contacts', selectedFile) // can be from html <input type="file" />
formData.append('sender_id', 1) //see the ID of the sender-ID on system dashboard
formData.append('message', "Hello World! I am sending my first SMS message using Notify Africa API to contacts in a file")
const response = await fetch(`${baseUrl}/smses/file/send`, {
method: 'POST',
body: formData,
headers: {
'Authorization': `Bearer ${token}`
},
});
const json = await response.json()
if (response.ok) {
console.log(json.message)
}
if (!response.ok) {
console.log(json.message);
}
} catch (err) {
console.log('Error while uploading file');
}
}
<?php
$token = 'YOUR_API_TOKEN';
$baseUrl = 'https://example.com/v2';
$url = "$baseUrl/smses/file/send";
$filePath = 'path/to/your/file.csv'; // Update with the actual file path
try {
$file = new CURLFile($filePath, 'text/csv');
$postFields = array(
'contacts' => $file,
'sender_id' => 1,
'message' => 'Hello World! I am sending my first SMS message using Notify Africa API to contacts in a file'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token"
));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$responseData = json_decode($response, true);
if ($httpCode == 200) {
echo "Response: ";
print_r($responseData);
} else {
echo "Error: ";
print_r($responseData);
}
} catch (Exception $e) {
echo 'Error while uploading file: ', $e->getMessage(), "\n";
}
?>
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class SendFileMessage {
public static void main(String[] args) {
String token = "YOUR_API_TOKEN";
String baseUrl = "https://example.com/v2";
String url = baseUrl + "/smses/file/send";
String filePath = "path/to/your/file.csv"; // Update with the actual file path
try {
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] fileBytes = new byte[(int) file.length()];
fileInputStream.read(fileBytes);
fileInputStream.close();
String boundary = "===" + System.currentTimeMillis() + "===";
String lineEnd = "\r\n";
String twoHyphens = "--";
URL apiUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) apiUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setDoOutput(true);
DataOutputStream request = new DataOutputStream(conn.getOutputStream());
// Add file part
request.writeBytes(twoHyphens + boundary + lineEnd);
request.writeBytes("Content-Disposition: form-data; name=\"contacts\"; filename=\"" + file.getName() + "\"" + lineEnd);
request.writeBytes("Content-Type: text/csv" + lineEnd);
request.writeBytes(lineEnd);
request.write(fileBytes);
request.writeBytes(lineEnd);
// Add sender_id part
request.writeBytes(twoHyphens + boundary + lineEnd);
request.writeBytes("Content-Disposition: form-data; name=\"sender_id\"" + lineEnd);
request.writeBytes(lineEnd);
request.writeBytes("1" + lineEnd);
// Add message part
request.writeBytes(twoHyphens + boundary + lineEnd);
request.writeBytes("Content-Disposition: form-data; name=\"message\"" + lineEnd);
request.writeBytes(lineEnd);
request.writeBytes("Hello World! I am sending my first SMS message using Notify Africa API to contacts in a file" + lineEnd);
// End of multipart/form-data.
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
request.flush();
request.close();
// Get the response code
int responseCode = conn.getResponseCode();
// Read the response body
BufferedReader reader;
if (responseCode == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// Check if the request was successful
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("Response: " + response.toString());
} else {
System.out.println("Error: " + response.toString());
}
// Close the connection
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Request Parameters
The following parameters are required when sending an SMS message:
contacts
: The file containing the contacts to send the message to.sender_id
: The ID of the sender ID to use when sending the message.message
: The message to send to the contacts in the file.Content-Type
: multipart/form-data