package main
import (
"context"
"fmt"
"log"
"os"
"github.com/AhaSend/ahasend-go"
"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.SubAccountsAPI.CreateSubAccount(ctx, accountID, requests.CreateSubAccountRequest{
Name: "Acme Subsidiary",
Website: "acme.example.com",
MonthlyCredit: ahasend.Int64(0),
})
if err != nil {
log.Fatalf("Error creating sub account: %v", err)
}
fmt.Printf("Created sub account %s\n", response.ID)
}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 subAccount = await client.subAccounts.create({
name: "Example subsidiary",
website: "subsidiary.example.com",
});
console.log("Sub-account created.", { id: subAccount.id, status: subAccount.status });
curl --request POST \
--url https://api.ahasend.com/v2/accounts/{account_id}/sub-accounts \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "Acme Subsidiary",
"website": "acme.example.com",
"monthly_credit": 0
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/sub-accounts"
payload = {
"name": "Acme Subsidiary",
"website": "acme.example.com",
"monthly_credit": 0
}
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}/sub-accounts",
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' => 'Acme Subsidiary',
'website' => 'acme.example.com',
'monthly_credit' => 0
]),
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}/sub-accounts")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"Acme Subsidiary\",\n \"website\": \"acme.example.com\",\n \"monthly_credit\": 0\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/sub-accounts")
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\": \"Acme Subsidiary\",\n \"website\": \"acme.example.com\",\n \"monthly_credit\": 0\n}"
response = http.request(request)
puts response.read_body{
"object": "sub_account",
"id": "2f3c5d2a-9ef8-4c91-a5f4-79990c8c1d3a",
"parent_account_id": "9d0cf9d0-4f5e-4674-bcf1-8ec39968b6e1",
"name": "Acme Subsidiary",
"website": "acme.example.com",
"status": "active",
"monthly_credit": 0,
"created_at": "2024-01-01T00:00:00Z",
"domain_count": 2,
"member_count": 3,
"last_activity_at": "2024-01-15T12:00:00Z"
}{
"message": "Error message"
}{
"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 a Sub-Account via the API
Create an isolated AhaSend sub-account under your parent account through the API, with a name, website and an optional monthly sending cap.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/AhaSend/ahasend-go"
"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.SubAccountsAPI.CreateSubAccount(ctx, accountID, requests.CreateSubAccountRequest{
Name: "Acme Subsidiary",
Website: "acme.example.com",
MonthlyCredit: ahasend.Int64(0),
})
if err != nil {
log.Fatalf("Error creating sub account: %v", err)
}
fmt.Printf("Created sub account %s\n", response.ID)
}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 subAccount = await client.subAccounts.create({
name: "Example subsidiary",
website: "subsidiary.example.com",
});
console.log("Sub-account created.", { id: subAccount.id, status: subAccount.status });
curl --request POST \
--url https://api.ahasend.com/v2/accounts/{account_id}/sub-accounts \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "Acme Subsidiary",
"website": "acme.example.com",
"monthly_credit": 0
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/sub-accounts"
payload = {
"name": "Acme Subsidiary",
"website": "acme.example.com",
"monthly_credit": 0
}
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}/sub-accounts",
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' => 'Acme Subsidiary',
'website' => 'acme.example.com',
'monthly_credit' => 0
]),
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}/sub-accounts")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"Acme Subsidiary\",\n \"website\": \"acme.example.com\",\n \"monthly_credit\": 0\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/sub-accounts")
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\": \"Acme Subsidiary\",\n \"website\": \"acme.example.com\",\n \"monthly_credit\": 0\n}"
response = http.request(request)
puts response.read_body{
"object": "sub_account",
"id": "2f3c5d2a-9ef8-4c91-a5f4-79990c8c1d3a",
"parent_account_id": "9d0cf9d0-4f5e-4674-bcf1-8ec39968b6e1",
"name": "Acme Subsidiary",
"website": "acme.example.com",
"status": "active",
"monthly_credit": 0,
"created_at": "2024-01-01T00:00:00Z",
"domain_count": 2,
"member_count": 3,
"last_activity_at": "2024-01-15T12:00:00Z"
}{
"message": "Error message"
}{
"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"
}Related guides
- API authentication, Permission scopes, Sub-account setup.
- Sub-account API rules, Sub-account CLI, Child API keys.
- 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
Sub account created successfully

