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()
campaignID := uuid.MustParse("11111111-1111-4111-8111-111111111111")
// The copy is a new draft. Leave Name nil to name it "Copy of" and the
// source's name.
response, _, err := client.CampaignsAPI.DuplicateCampaign(ctx, accountID, campaignID, requests.DuplicateCampaignRequest{
Name: ahasend.String("November newsletter"),
})
if err != nil {
log.Fatalf("Error duplicating campaign: %v", err)
}
fmt.Printf("Created draft %s %s\n", response.ID, response.Name)
}
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 campaignId = "00000000-0000-4000-8000-000000000014";
const copy = await client.campaigns.duplicate(campaignId, { name: "November newsletter" });
console.log("Campaign duplicated.", { id: copy.id, status: copy.status });
curl --request POST \
--url https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}/duplicate \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "<string>"
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}/duplicate"
payload = { "name": "<string>" }
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}/campaigns/{campaign_id}/duplicate",
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' => '<string>'
]),
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}/campaigns/{campaign_id}/duplicate")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}/duplicate")
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\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"object": "campaign",
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"name": "<string>",
"subject": "<string>",
"preheader": "<string>",
"from": {
"email": "noreply@example.com",
"name": "Example Corp"
},
"reply_to": "jsmith@example.com",
"status": "draft",
"status_reason": "<string>",
"send_at": "2023-11-07T05:31:56Z",
"send_resumes_at": "2023-11-07T05:31:56Z",
"finished_at": "2023-11-07T05:31:56Z",
"list_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"segment_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"exclude_list_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"exclude_segment_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"editor": "advanced",
"content": {
"text_is_custom": true,
"mjml": "<string>",
"html": "<string>",
"text": "<string>",
"markup": "<Email version=\"1\">\n <Heading level=\"3\">Welcome</Heading>\n <Paragraph>Thanks for signing up.</Paragraph>\n</Email>\n",
"markup_error": {
"code": "html_block",
"message": "The design holds an HTML block, which AhaML cannot show; remove the block in the dashboard to read and write the design as AhaML"
}
},
"ready": true,
"issues": [
{
"code": "sender_missing",
"message": "<string>"
}
],
"markup_warnings": [
{
"code": "<string>",
"message": "<string>",
"line": 2,
"column": 2,
"path": "<string>",
"hint": "<string>"
}
]
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "The campaign's audience is being updated. Try again in a minute."
}{
"message": "idempotency key was already used with a different request payload"
}{
"message": "Error message"
}{
"message": "Error message"
}Duplicate a Campaign via the API
Make a new draft from an AhaSend marketing campaign in any status, with a copy of its design, its subject, preview text and sender, and the same audience.
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()
campaignID := uuid.MustParse("11111111-1111-4111-8111-111111111111")
// The copy is a new draft. Leave Name nil to name it "Copy of" and the
// source's name.
response, _, err := client.CampaignsAPI.DuplicateCampaign(ctx, accountID, campaignID, requests.DuplicateCampaignRequest{
Name: ahasend.String("November newsletter"),
})
if err != nil {
log.Fatalf("Error duplicating campaign: %v", err)
}
fmt.Printf("Created draft %s %s\n", response.ID, response.Name)
}
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 campaignId = "00000000-0000-4000-8000-000000000014";
const copy = await client.campaigns.duplicate(campaignId, { name: "November newsletter" });
console.log("Campaign duplicated.", { id: copy.id, status: copy.status });
curl --request POST \
--url https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}/duplicate \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "<string>"
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}/duplicate"
payload = { "name": "<string>" }
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}/campaigns/{campaign_id}/duplicate",
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' => '<string>'
]),
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}/campaigns/{campaign_id}/duplicate")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}/duplicate")
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\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"object": "campaign",
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"name": "<string>",
"subject": "<string>",
"preheader": "<string>",
"from": {
"email": "noreply@example.com",
"name": "Example Corp"
},
"reply_to": "jsmith@example.com",
"status": "draft",
"status_reason": "<string>",
"send_at": "2023-11-07T05:31:56Z",
"send_resumes_at": "2023-11-07T05:31:56Z",
"finished_at": "2023-11-07T05:31:56Z",
"list_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"segment_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"exclude_list_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"exclude_segment_ids": [
"3c90c3cc-0d44-4b50-8888-8dd25736052a"
],
"editor": "advanced",
"content": {
"text_is_custom": true,
"mjml": "<string>",
"html": "<string>",
"text": "<string>",
"markup": "<Email version=\"1\">\n <Heading level=\"3\">Welcome</Heading>\n <Paragraph>Thanks for signing up.</Paragraph>\n</Email>\n",
"markup_error": {
"code": "html_block",
"message": "The design holds an HTML block, which AhaML cannot show; remove the block in the dashboard to read and write the design as AhaML"
}
},
"ready": true,
"issues": [
{
"code": "sender_missing",
"message": "<string>"
}
],
"markup_warnings": [
{
"code": "<string>",
"message": "<string>",
"line": 2,
"column": 2,
"path": "<string>",
"hint": "<string>"
}
]
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "The campaign's audience is being updated. Try again in a minute."
}{
"message": "idempotency key was already used with a different request payload"
}{
"message": "Error message"
}{
"message": "Error message"
}campaigns:write.
The source campaign can be in any status, so you can copy a campaign that was already sent. The new draft gets:
- A copy of the design. The copy belongs to the new draft.
- The subject, the preview text, the sender and the reply-to.
- The lists and segments of the source that still exist, included and excluded, with the included lists in the same order. When none of the included lists and segments still exists, the new draft has no lists or segments.
name sets the name of the new draft. Without it, the name is Copy of and the name of the source, cut to 255 characters. The request body is optional.
This operation accepts an Idempotency-Key header. See Idempotency.
Related guides
Authorizations
Headers
Optional idempotency key for safe request retries. Must be a unique string for each logical request.
An identical request with a completed stored outcome returns the original status and body. An in-progress
execution returns 409, a changed method/path/body returns 422, and a released 5xx execution may run again.
Keys for non-secret responses expire after 24 hours. API-key create responses include a one-time secret_key,
so successful encrypted replay responses for those operations expire after 5 minutes.
255Body
Response
Campaign duplicated
A campaign as List Campaigns returns it, without its audience, content and issues.
Where the campaign is in its life. Only a draft can be changed or
deleted.
draft: being prepared.validating: scheduled, and its lists and segments are being checked.waiting_for_review: held for a review before it sends.scheduled: accepted, waiting for its send time.sending: sending now.paused: sending stopped for now;status_reasonsays why.sent: every message was sent.canceled: stopped before it finished;status_reasonsays why.failed: could not be sent;status_reasonsays why.
draft, validating, waiting_for_review, scheduled, sending, paused, sent, canceled, failed 
