Create Contact
Voilà! You are about to create your first contact on Notify Africa.
What is a Contact?
A contact is a unique identifier that represents a recipient of an SMS message. It is a great way to store and manage your contacts and send personalized messages to your customers and stakeholders.
Contacts can be individuals, groups, or organizations that you want to communicate with using SMS messages.
- Javascript
- PHP
- Java
try {
// Send a POST request to the API endpoint for creating contacts
const baseUrl = 'https://example.com/v2'; // contact us for the base URL
const token = 'YOUR_API_TOKEN';
const endPoint = '/contacts/create';
const url = `${baseUrl}${endPoint}`;
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify({
contacts: [{
names: 'John Doe',
number: '255xxxxxxx',
email: 'john@doe.com'
}]
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${token}`
}
});
// Parse the response body as JSON
const json = await response.json();
console.log('Response:', json);
} catch (error) {
console.error('Error:', error);
}
<?php
// API endpoint URL
$url = 'https://example.com/v2/contacts/create';
// Data to be sent in the request body
$data = array(
'contacts' => array(
array(
'names' => 'John Doe',
'number' => '255xxxxxxx',
'email' => 'john@doe.com'
)
)
);
// Set the headers and request options
$options = array(
'http' => array(
'header' => "Content-Type: application/json" .
"Accept: application/json" .
"Authorization: Bearer YOUR_API_TOKEN",
'method' => 'POST',
'content' => json_encode($data)
)
);
// Create a stream context
$context = stream_context_create($options);
// Send the request and get the response
$response = file_get_contents($url, false, $context);
// Parse the response JSON
$responseData = json_decode($response, true);
// Check if the response is successful
if (isset($responseData['success']) && $responseData['success']) {
echo "New contact added successfully.";
// Additional actions upon successful contact creation
} else {
echo "Error: " . $responseData['message'];
// Handle the error response
}
?>
In this simplified version, we use the file_get_contents
function along with a stream context to send a POST
request to the specified API endpoint (hhttps://api.hudumasms.com/v2/contacts/create
). The request body is set as JSON using json_encode
, and the necessary headers are included in the context options. After receiving the response, we decode the JSON and check if the request was successful. If successful, we display a success message. Otherwise, we handle the error response by displaying the corresponding error message.
Please note that this simplified version uses the file_get_contents
function, which requires the allow_url_fopen
setting to be enabled on your server.
Here's a version of the Java code snippet for adding a new contact to the HudumaSMS system:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class AddContactExample {
public static void main(String[] args) {
// API endpoint URL
String apiUrl = "https://example.com/v2/contacts/create";
// Request body data
String requestBody = "{\"contacts\": [{\"names\": \"John Doe\", \"number\": \"255xxxxxxx\", \"email\": \"john@doe.com\"}]}";
try {
// Create URL object
URL url = new URL(apiUrl);
// Create HTTP connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer YOUR_API_TOKEN"); // Replace with your API token
conn.setDoOutput(true);
// Write the request body
OutputStream outputStream = conn.getOutputStream();
outputStream.write(requestBody.getBytes());
outputStream.flush();
// 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("New contact added successfully.");
// Additional actions upon successful contact creation
} else {
System.out.println("Error: " + response.toString());
// Handle the error response
}
// Close the connection
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Please note that you'll need to replace the placeholder values in the requestBody
and "Bearer YOUR_API_TOKEN"
with the actual values for the contact you want to add and your Account API token, respectively.