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")
// Only the fields you set change, and only while the campaign is a
// draft. A ReplyTo that points at "" clears the reply-to address.
response, _, err := client.CampaignsAPI.UpdateCampaign(ctx, accountID, campaignID, requests.UpdateCampaignRequest{
Subject: ahasend.String("Our news for October"),
ReplyTo: ahasend.String(""),
})
if err != nil {
log.Fatalf("Error updating campaign: %v", err)
}
fmt.Printf("Updated campaign %s, %d issues left\n", response.Name, len(response.Issues))
}
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";
// Only a draft can change; null clears the reply-to address.
const campaign = await client.campaigns.update(campaignId, {
subject: "What we shipped in October",
reply_to: null,
});
console.log("Campaign updated.", { id: campaign.id, ready: campaign.ready });
curl --request PUT \
--url https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id} \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "<string>",
"subject": "<string>",
"preheader": "<string>",
"from": {
"email": "jsmith@example.com",
"name": "<string>"
},
"reply_to": "jsmith@example.com",
"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"
],
"content": {
"mjml": "<string>",
"html": "<string>",
"text": "<string>",
"text_is_custom": true,
"markup": "<Email version=\"1\">\n <Heading level=\"3\">Welcome</Heading>\n <Paragraph>Thanks for signing up.</Paragraph>\n</Email>\n",
"markup_error": "<unknown>"
}
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}"
payload = {
"name": "<string>",
"subject": "<string>",
"preheader": "<string>",
"from": {
"email": "jsmith@example.com",
"name": "<string>"
},
"reply_to": "jsmith@example.com",
"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"],
"content": {
"mjml": "<string>",
"html": "<string>",
"text": "<string>",
"text_is_custom": True,
"markup": "<Email version=\"1\">
<Heading level=\"3\">Welcome</Heading>
<Paragraph>Thanks for signing up.</Paragraph>
</Email>
",
"markup_error": "<unknown>"
}
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.put(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}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode([
'name' => '<string>',
'subject' => '<string>',
'preheader' => '<string>',
'from' => [
'email' => 'jsmith@example.com',
'name' => '<string>'
],
'reply_to' => 'jsmith@example.com',
'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'
],
'content' => [
'mjml' => '<string>',
'html' => '<string>',
'text' => '<string>',
'text_is_custom' => true,
'markup' => '<Email version="1">
<Heading level="3">Welcome</Heading>
<Paragraph>Thanks for signing up.</Paragraph>
</Email>
',
'markup_error' => '<unknown>'
]
]),
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.put("https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\",\n \"subject\": \"<string>\",\n \"preheader\": \"<string>\",\n \"from\": {\n \"email\": \"jsmith@example.com\",\n \"name\": \"<string>\"\n },\n \"reply_to\": \"jsmith@example.com\",\n \"list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"content\": {\n \"mjml\": \"<string>\",\n \"html\": \"<string>\",\n \"text\": \"<string>\",\n \"text_is_custom\": true,\n \"markup\": \"<Email version=\\\"1\\\">\\n <Heading level=\\\"3\\\">Welcome</Heading>\\n <Paragraph>Thanks for signing up.</Paragraph>\\n</Email>\\n\",\n \"markup_error\": \"<unknown>\"\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"<string>\",\n \"subject\": \"<string>\",\n \"preheader\": \"<string>\",\n \"from\": {\n \"email\": \"jsmith@example.com\",\n \"name\": \"<string>\"\n },\n \"reply_to\": \"jsmith@example.com\",\n \"list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"content\": {\n \"mjml\": \"<string>\",\n \"html\": \"<string>\",\n \"text\": \"<string>\",\n \"text_is_custom\": true,\n \"markup\": \"<Email version=\\\"1\\\">\\n <Heading level=\\\"3\\\">Welcome</Heading>\\n <Paragraph>Thanks for signing up.</Paragraph>\\n</Email>\\n\",\n \"markup_error\": \"<unknown>\"\n }\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>"
}
]
}Update a Campaign via the API
Change a draft AhaSend marketing campaign: its name, sender, reply-to, audience, subject, preview text or design. Only a draft campaign can be changed.
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")
// Only the fields you set change, and only while the campaign is a
// draft. A ReplyTo that points at "" clears the reply-to address.
response, _, err := client.CampaignsAPI.UpdateCampaign(ctx, accountID, campaignID, requests.UpdateCampaignRequest{
Subject: ahasend.String("Our news for October"),
ReplyTo: ahasend.String(""),
})
if err != nil {
log.Fatalf("Error updating campaign: %v", err)
}
fmt.Printf("Updated campaign %s, %d issues left\n", response.Name, len(response.Issues))
}
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";
// Only a draft can change; null clears the reply-to address.
const campaign = await client.campaigns.update(campaignId, {
subject: "What we shipped in October",
reply_to: null,
});
console.log("Campaign updated.", { id: campaign.id, ready: campaign.ready });
curl --request PUT \
--url https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id} \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "<string>",
"subject": "<string>",
"preheader": "<string>",
"from": {
"email": "jsmith@example.com",
"name": "<string>"
},
"reply_to": "jsmith@example.com",
"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"
],
"content": {
"mjml": "<string>",
"html": "<string>",
"text": "<string>",
"text_is_custom": true,
"markup": "<Email version=\"1\">\n <Heading level=\"3\">Welcome</Heading>\n <Paragraph>Thanks for signing up.</Paragraph>\n</Email>\n",
"markup_error": "<unknown>"
}
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}"
payload = {
"name": "<string>",
"subject": "<string>",
"preheader": "<string>",
"from": {
"email": "jsmith@example.com",
"name": "<string>"
},
"reply_to": "jsmith@example.com",
"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"],
"content": {
"mjml": "<string>",
"html": "<string>",
"text": "<string>",
"text_is_custom": True,
"markup": "<Email version=\"1\">
<Heading level=\"3\">Welcome</Heading>
<Paragraph>Thanks for signing up.</Paragraph>
</Email>
",
"markup_error": "<unknown>"
}
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.put(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}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode([
'name' => '<string>',
'subject' => '<string>',
'preheader' => '<string>',
'from' => [
'email' => 'jsmith@example.com',
'name' => '<string>'
],
'reply_to' => 'jsmith@example.com',
'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'
],
'content' => [
'mjml' => '<string>',
'html' => '<string>',
'text' => '<string>',
'text_is_custom' => true,
'markup' => '<Email version="1">
<Heading level="3">Welcome</Heading>
<Paragraph>Thanks for signing up.</Paragraph>
</Email>
',
'markup_error' => '<unknown>'
]
]),
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.put("https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\",\n \"subject\": \"<string>\",\n \"preheader\": \"<string>\",\n \"from\": {\n \"email\": \"jsmith@example.com\",\n \"name\": \"<string>\"\n },\n \"reply_to\": \"jsmith@example.com\",\n \"list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"content\": {\n \"mjml\": \"<string>\",\n \"html\": \"<string>\",\n \"text\": \"<string>\",\n \"text_is_custom\": true,\n \"markup\": \"<Email version=\\\"1\\\">\\n <Heading level=\\\"3\\\">Welcome</Heading>\\n <Paragraph>Thanks for signing up.</Paragraph>\\n</Email>\\n\",\n \"markup_error\": \"<unknown>\"\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/campaigns/{campaign_id}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"<string>\",\n \"subject\": \"<string>\",\n \"preheader\": \"<string>\",\n \"from\": {\n \"email\": \"jsmith@example.com\",\n \"name\": \"<string>\"\n },\n \"reply_to\": \"jsmith@example.com\",\n \"list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_list_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"exclude_segment_ids\": [\n \"3c90c3cc-0d44-4b50-8888-8dd25736052a\"\n ],\n \"content\": {\n \"mjml\": \"<string>\",\n \"html\": \"<string>\",\n \"text\": \"<string>\",\n \"text_is_custom\": true,\n \"markup\": \"<Email version=\\\"1\\\">\\n <Heading level=\\\"3\\\">Welcome</Heading>\\n <Paragraph>Thanks for signing up.</Paragraph>\\n</Email>\\n\",\n \"markup_error\": \"<unknown>\"\n }\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>"
}
]
}campaigns:write.
A campaign that is not a draft answers 409 with campaign is not a draft. To change a scheduled campaign, unschedule it first. See Changes only to drafts.
Fields:
- A field that you leave out does not change and is not checked. A request with no field is refused with
400. nullclearssubject,preheader,fromorreply_to. When you clearfrom,reply_tostays.list_ids,segment_ids,exclude_list_idsandexclude_segment_idseach replace their own set when you send them, and leave it as it is when you leave them out.[]empties a set, soexclude_segment_ids: []removes every segment exclusion. The result needs at least one list or segment to send to. See Audience.- A list or segment that does not exist is refused with
400. When a segment of the audience is being built again, the API answers409withThe campaign's audience is being updated. Try again in a minute.This refusal is not stored: it releases theIdempotency-Key, so a retry with the same key runs again. Retry it after a minute. template_idis not accepted: only Create Campaign can copy a campaign template. A request withtemplate_idis refused with400as an unknown field.- The fields follow the rules of Create Campaign.
- You cannot change the editor of the design.
- A campaign with no design yet needs
content.mjml,content.htmlorcontent.markup, which sets its editor as in Create Campaign. - For a
simpledesign, change the design incontent.markup, as AhaML: you cannot change itscontent.html. - When you send a new
content.mjml,content.htmlorcontent.markupand leave outcontent.text, a text written by hand stays, and a text made from the HTML is made again from the new HTML. Sendcontent.textasnullto make the text again from the new HTML, also when the text was written by hand. - A text written by hand that stays must still render. If it holds a
{{,{%or{#that cannot be rendered, the request is refused with400. - The content follows the content rules of Create Campaign.
- The API and the dashboard change the same campaign. The request waits for other changes to the same campaign to finish, and a send that starts after the request uses the change.
- When a dashboard editor is open on the same campaign, the design that is saved last is kept.
Idempotency-Key header. Except for the audience 409, a refusal from this operation is stored for the key, so send a corrected request with a new key. See Idempotency and campaign retries.
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
Changes to a draft campaign. A field left out is neither changed nor checked. The design's editor never changes.
Response
Campaign updated
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 
