Transactional emails play a crucial role in communicating with your users. Whether it’s order confirmations, password resets, or important notifications, these emails need to be sent reliably and efficiently. In this article, we will guide you through the setup and integration of Sweego’s transactional email API using the /send
method.
Introduction to the Sweego API
The Sweego API offers a simple yet powerful solution for sending transactional emails. With its robust features, you can easily integrate transactional email sending into your application. You can find the complete API documentation here.
Prerequisites
Before you begin, make sure you have:
- A Sweego account with API access. https://app.sweego.io/signup
- An API key generated from your Sweego dashboard.
- A development environment ready to make HTTP requests (such as Postman, cURL, or a web application).
Step 1: Obtain the API Key
Log into your Sweego account and navigate to the Email menu in your dashboard, then API and click on Generate API Key. Generate a new API key and make a note of it, as you’ll need it to authenticate your requests and, more importantly, we won’t be able to show it to you a second time for security reasons.
Step 2: Setting Up the Environment
Depending on the programming language you use, you can set up your environment to send HTTP requests. Here are examples in a few common languages.
curl
Python
Node
PHP
curl --location 'https://api.sweego.io/send' \
--header 'Content-Type: application/json' \
--header 'Api-Key: <API_KEY>' \
--data-raw '{
"channel": "email",
"provider": "sweego",
"recipients": [
{ "email": "<EMAIL_TO>" }
],
"from": {
"name": "MY NAME",
"email": "<EMAIL_FROM>"
},
"subject": "Email subject",
"message-txt": "Email body"
}'
import requests
api_url = "https://api.sweego.io/send"
api_key = "votre_clé_API"
email_data = {
"channel": "email",
"provider": "sweego",
"recipients": [{"email": "destinataire@example.com"}],
"from": {"name": "MY NAME", "email": "votre_email@example.com"},
"subject": "Email subject",
"message-txt": "Email body"
}
headers = {
"Api-Key": api_key,
"Content-Type": "application/json"
}
response = requests.post(api_url, json=email_data, headers=headers)
if response.status_code == 200:
print("Email envoyé avec succès!")
else:
print(f"Erreur lors de l'envoi de l'email: {response.status_code}")
const fetch = require('node-fetch');
const apiUrl = "https://api.sweego.io/send";
const apiKey = "votre_clé_API";
const emailData = {
channel: "email",
provider: "sweego",
recipients: [{ email: "destinataire@example.com" }],
from: { name: "MY NAME", email: "votre_email@example.com" },
subject: "Email subject",
"message-txt": "Email body"
};
const headers = {
"Api-Key": apiKey,
"Content-Type": "application/json"
};
fetch(apiUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(emailData)
})
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error(`Erreur lors de l'envoi de l'email: ${response.status}`);
})
.then(data => {
console.log("Email envoyé avec succès!");
})
.catch(error => {
console.error(error);
});
<?php
$api_url = "https://api.sweego.io/send";
$api_key = "votre_clé_API";
$email_data = [
"channel" => "email",
"provider" => "sweego",
"recipients" => [
["email" => "destinataire@example.com"]
],
"from" => [
"name" => "MY NAME",
"email" => "votre_email@example.com"
],
"subject" => "Email subject",
"message-txt" => "Email body"
];
$headers = [
"Api-Key: $api_key",
"Content-Type: application/json"
];
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($email_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code == 200) {
echo "Email envoyé avec succès!";
} else {
echo "Erreur lors de l'envoi de l'email: $http_code";
}
?>
Step 3: Sending an Email
Using the code examples above, you can send a transactional email by calling the /send
method of the Sweego API. Make sure to replace <API_KEY>
, <EMAIL_TO>
, and <EMAIL_FROM>
with your actual information and customize the subject
and message-txt
fields according to your needs.
Step 4: Handling Responses and Errors
It’s important to properly handle API responses and manage potential errors. The Sweego API returns standard HTTP status codes to indicate the success or failure of a request. Ensure you check these codes and take appropriate actions in case of errors.
Conclusion
Integrating Sweego’s transactional email API into your application is a straightforward process that can significantly improve the reliability and efficiency of your email communications. By following the steps outlined in this article, you will be able to send transactional emails quickly and securely.
For more details on advanced features and API parameters, refer to the complete Sweego API documentation.
Leave a Reply