package main
import (
"context"
"fmt"
"log"
"os"
"github.com/AhaSend/ahasend-go/api"
"github.com/AhaSend/ahasend-go/models/requests"
"github.com/google/uuid"
)
func main() {
client := api.NewAPIClient(api.WithAPIKey(os.Getenv("AHASEND_API_KEY")))
accountID := uuid.MustParse(os.Getenv("AHASEND_ACCOUNT_ID"))
ctx := context.Background()
// The SDK sends a fresh Idempotency-Key with this POST and reuses it on
// its own retries, so a retried request is not applied twice.
response, _, err := client.SMTPCredentialsAPI.CreateSMTPCredential(ctx, accountID, requests.CreateSMTPCredentialRequest{
Name: "My SMTP Credential",
Scope: "global",
Sandbox: true,
})
if err != nil {
log.Fatalf("Error creating SMTP credential: %v", err)
}
// response.Password is returned only on create: put it in your secret
// store now, and never log it.
fmt.Printf("Created SMTP credential for username %s\n", response.Username)
}import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
// The SDK sends a fresh Idempotency-Key automatically and reuses it on its own retries.
const credential = await client.smtpCredentials.create({
name: "Production SMTP",
scope: "global",
});
console.log("SMTP credential created.", { id: credential.id, name: credential.name });
curl --request POST \
--url https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "Production SMTP",
"scope": "global"
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials"
payload = {
"name": "Production SMTP",
"scope": "global"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'Production SMTP',
'scope' => 'global'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.post("https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"Production SMTP\",\n \"scope\": \"global\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"Production SMTP\",\n \"scope\": \"global\"\n}"
response = http.request(request)
puts response.read_body{
"object": "credential_smtp",
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"name": "<string>",
"username": "<string>",
"sandbox": true,
"scope": "global",
"domains": [
"<string>"
],
"password": "<string>"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "A request with this idempotency key is already in progress"
}{
"message": "idempotency key was already used with a different request payload"
}{
"message": "Error message"
}Create SMTP Credentials via the API
Create SMTP username and password credentials through the AhaSend API to authenticate with the SMTP relay, scoped to specific sending domains or global.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/AhaSend/ahasend-go/api"
"github.com/AhaSend/ahasend-go/models/requests"
"github.com/google/uuid"
)
func main() {
client := api.NewAPIClient(api.WithAPIKey(os.Getenv("AHASEND_API_KEY")))
accountID := uuid.MustParse(os.Getenv("AHASEND_ACCOUNT_ID"))
ctx := context.Background()
// The SDK sends a fresh Idempotency-Key with this POST and reuses it on
// its own retries, so a retried request is not applied twice.
response, _, err := client.SMTPCredentialsAPI.CreateSMTPCredential(ctx, accountID, requests.CreateSMTPCredentialRequest{
Name: "My SMTP Credential",
Scope: "global",
Sandbox: true,
})
if err != nil {
log.Fatalf("Error creating SMTP credential: %v", err)
}
// response.Password is returned only on create: put it in your secret
// store now, and never log it.
fmt.Printf("Created SMTP credential for username %s\n", response.Username)
}import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
// The SDK sends a fresh Idempotency-Key automatically and reuses it on its own retries.
const credential = await client.smtpCredentials.create({
name: "Production SMTP",
scope: "global",
});
console.log("SMTP credential created.", { id: credential.id, name: credential.name });
curl --request POST \
--url https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "Production SMTP",
"scope": "global"
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials"
payload = {
"name": "Production SMTP",
"scope": "global"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'Production SMTP',
'scope' => 'global'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.post("https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"Production SMTP\",\n \"scope\": \"global\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/smtp-credentials")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"Production SMTP\",\n \"scope\": \"global\"\n}"
response = http.request(request)
puts response.read_body{
"object": "credential_smtp",
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"name": "<string>",
"username": "<string>",
"sandbox": true,
"scope": "global",
"domains": [
"<string>"
],
"password": "<string>"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "A request with this idempotency key is already in progress"
}{
"message": "idempotency key was already used with a different request payload"
}{
"message": "Error message"
}scoped
credential requires at least one domain and the key must have write
permission for every supplied domain. A global credential requires
smtp-credentials:write:all; supplied domains are accepted but ignored,
and the response contains domains: []. SMTP credentials have no
update endpoint.
Related guides
- API authentication, Permission scopes, SMTP credential setup.
- SMTP credential CLI, SMTP settings, API errors.
- API rate limits, Plans and limits.
Authorizations
Headers
Optional idempotency key for safe request retries. Must be a unique string for each logical request.
A request with a key does its operation at most one time during the life of the key, so a retry with the
same key and the same request is safe after a 5xx, a timeout or a lost answer. An identical request with a
completed stored outcome returns the original status and body. An in-progress execution returns 409, and a
changed method/path/body returns 422. A key lives 24 hours from the first request that uses it. API-key create
responses include a one-time secret_key, so on those operations a key and its encrypted replay response
live 5 minutes.
255Body
Response
SMTP credential created successfully

