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()
segmentID := uuid.MustParse("11111111-1111-4111-8111-111111111111")
// Only the fields you set change. A new name or description keeps the
// members; a new definition or time zone builds them again.
response, _, err := client.SegmentsAPI.UpdateSegment(ctx, accountID, segmentID, requests.UpdateSegmentRequest{
Name: ahasend.String("Engaged customers"),
Description: ahasend.String("Opened a campaign recently"),
})
if err != nil {
log.Fatalf("Error updating segment: %v", err)
}
fmt.Printf("Updated segment %s (%s)\n", response.Name, response.State)
}
import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
const segmentId = "00000000-0000-4000-8000-000000000015";
// A changed definition or time zone builds the members again; a new name does not.
const segment = await client.segments.update(segmentId, { name: "Engaged German customers" });
console.log("Segment updated.", { id: segment.id, name: segment.name });
curl --request PUT \
--url https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id} \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "<string>",
"description": "<string>",
"definition": {
"schema_version": 1,
"match": "all",
"conditions": [
{
"field": "attributes.country",
"op": "equal",
"value": "DE"
},
{
"field": "created_at",
"op": "after",
"value": "30 days ago"
},
{
"field": "list",
"op": "in",
"value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b"
},
{
"match": "any",
"conditions": [
{
"field": "campaign",
"op": "opened",
"value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b"
},
{
"field": "any_campaign",
"op": "clicked",
"value": "14 days ago"
},
{
"field": "campaign_count",
"op": "opened_at_least",
"value": {
"count": 3,
"since": "90 days ago"
}
}
]
}
]
},
"timezone": "<string>"
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}"
payload = {
"name": "<string>",
"description": "<string>",
"definition": {
"schema_version": 1,
"match": "all",
"conditions": [
{
"field": "attributes.country",
"op": "equal",
"value": "DE"
},
{
"field": "created_at",
"op": "after",
"value": "30 days ago"
},
{
"field": "list",
"op": "in",
"value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b"
},
{
"match": "any",
"conditions": [
{
"field": "campaign",
"op": "opened",
"value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b"
},
{
"field": "any_campaign",
"op": "clicked",
"value": "14 days ago"
},
{
"field": "campaign_count",
"op": "opened_at_least",
"value": {
"count": 3,
"since": "90 days ago"
}
}
]
}
]
},
"timezone": "<string>"
}
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}/segments/{segment_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>',
'description' => '<string>',
'definition' => [
'schema_version' => 1,
'match' => 'all',
'conditions' => [
[
'field' => 'attributes.country',
'op' => 'equal',
'value' => 'DE'
],
[
'field' => 'created_at',
'op' => 'after',
'value' => '30 days ago'
],
[
'field' => 'list',
'op' => 'in',
'value' => '6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b'
],
[
'match' => 'any',
'conditions' => [
[
'field' => 'campaign',
'op' => 'opened',
'value' => '9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b'
],
[
'field' => 'any_campaign',
'op' => 'clicked',
'value' => '14 days ago'
],
[
'field' => 'campaign_count',
'op' => 'opened_at_least',
'value' => [
'count' => 3,
'since' => '90 days ago'
]
]
]
]
]
],
'timezone' => '<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.put("https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"definition\": {\n \"schema_version\": 1,\n \"match\": \"all\",\n \"conditions\": [\n {\n \"field\": \"attributes.country\",\n \"op\": \"equal\",\n \"value\": \"DE\"\n },\n {\n \"field\": \"created_at\",\n \"op\": \"after\",\n \"value\": \"30 days ago\"\n },\n {\n \"field\": \"list\",\n \"op\": \"in\",\n \"value\": \"6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b\"\n },\n {\n \"match\": \"any\",\n \"conditions\": [\n {\n \"field\": \"campaign\",\n \"op\": \"opened\",\n \"value\": \"9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b\"\n },\n {\n \"field\": \"any_campaign\",\n \"op\": \"clicked\",\n \"value\": \"14 days ago\"\n },\n {\n \"field\": \"campaign_count\",\n \"op\": \"opened_at_least\",\n \"value\": {\n \"count\": 3,\n \"since\": \"90 days ago\"\n }\n }\n ]\n }\n ]\n },\n \"timezone\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_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 \"description\": \"<string>\",\n \"definition\": {\n \"schema_version\": 1,\n \"match\": \"all\",\n \"conditions\": [\n {\n \"field\": \"attributes.country\",\n \"op\": \"equal\",\n \"value\": \"DE\"\n },\n {\n \"field\": \"created_at\",\n \"op\": \"after\",\n \"value\": \"30 days ago\"\n },\n {\n \"field\": \"list\",\n \"op\": \"in\",\n \"value\": \"6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b\"\n },\n {\n \"match\": \"any\",\n \"conditions\": [\n {\n \"field\": \"campaign\",\n \"op\": \"opened\",\n \"value\": \"9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b\"\n },\n {\n \"field\": \"any_campaign\",\n \"op\": \"clicked\",\n \"value\": \"14 days ago\"\n },\n {\n \"field\": \"campaign_count\",\n \"op\": \"opened_at_least\",\n \"value\": {\n \"count\": 3,\n \"since\": \"90 days ago\"\n }\n }\n ]\n }\n ]\n },\n \"timezone\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"object": "segment",
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"name": "<string>",
"description": "<string>",
"definition": {
"schema_version": 1,
"match": "all",
"conditions": [
{
"field": "attributes.country",
"op": "equal",
"value": "DE"
},
{
"field": "created_at",
"op": "after",
"value": "30 days ago"
},
{
"field": "list",
"op": "in",
"value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b"
},
{
"match": "any",
"conditions": [
{
"field": "campaign",
"op": "opened",
"value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b"
},
{
"field": "any_campaign",
"op": "clicked",
"value": "14 days ago"
},
{
"field": "campaign_count",
"op": "opened_at_least",
"value": {
"count": 3,
"since": "90 days ago"
}
}
]
}
]
},
"timezone": "<string>",
"state": "building",
"failure_reason": "<string>",
"member_count": 1,
"last_evaluated_at": "2023-11-07T05:31:56Z"
}{
"message": "conditions[0].field: the account has no contact field \"city\"; conditions[1].value: must be a list id"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}Update a Segment via the API
Change the name, description, rules or time zone of an AhaSend segment. New rules or a new time zone make AhaSend find the members of the segment again.
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()
segmentID := uuid.MustParse("11111111-1111-4111-8111-111111111111")
// Only the fields you set change. A new name or description keeps the
// members; a new definition or time zone builds them again.
response, _, err := client.SegmentsAPI.UpdateSegment(ctx, accountID, segmentID, requests.UpdateSegmentRequest{
Name: ahasend.String("Engaged customers"),
Description: ahasend.String("Opened a campaign recently"),
})
if err != nil {
log.Fatalf("Error updating segment: %v", err)
}
fmt.Printf("Updated segment %s (%s)\n", response.Name, response.State)
}
import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
const segmentId = "00000000-0000-4000-8000-000000000015";
// A changed definition or time zone builds the members again; a new name does not.
const segment = await client.segments.update(segmentId, { name: "Engaged German customers" });
console.log("Segment updated.", { id: segment.id, name: segment.name });
curl --request PUT \
--url https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id} \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"name": "<string>",
"description": "<string>",
"definition": {
"schema_version": 1,
"match": "all",
"conditions": [
{
"field": "attributes.country",
"op": "equal",
"value": "DE"
},
{
"field": "created_at",
"op": "after",
"value": "30 days ago"
},
{
"field": "list",
"op": "in",
"value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b"
},
{
"match": "any",
"conditions": [
{
"field": "campaign",
"op": "opened",
"value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b"
},
{
"field": "any_campaign",
"op": "clicked",
"value": "14 days ago"
},
{
"field": "campaign_count",
"op": "opened_at_least",
"value": {
"count": 3,
"since": "90 days ago"
}
}
]
}
]
},
"timezone": "<string>"
}
'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}"
payload = {
"name": "<string>",
"description": "<string>",
"definition": {
"schema_version": 1,
"match": "all",
"conditions": [
{
"field": "attributes.country",
"op": "equal",
"value": "DE"
},
{
"field": "created_at",
"op": "after",
"value": "30 days ago"
},
{
"field": "list",
"op": "in",
"value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b"
},
{
"match": "any",
"conditions": [
{
"field": "campaign",
"op": "opened",
"value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b"
},
{
"field": "any_campaign",
"op": "clicked",
"value": "14 days ago"
},
{
"field": "campaign_count",
"op": "opened_at_least",
"value": {
"count": 3,
"since": "90 days ago"
}
}
]
}
]
},
"timezone": "<string>"
}
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}/segments/{segment_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>',
'description' => '<string>',
'definition' => [
'schema_version' => 1,
'match' => 'all',
'conditions' => [
[
'field' => 'attributes.country',
'op' => 'equal',
'value' => 'DE'
],
[
'field' => 'created_at',
'op' => 'after',
'value' => '30 days ago'
],
[
'field' => 'list',
'op' => 'in',
'value' => '6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b'
],
[
'match' => 'any',
'conditions' => [
[
'field' => 'campaign',
'op' => 'opened',
'value' => '9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b'
],
[
'field' => 'any_campaign',
'op' => 'clicked',
'value' => '14 days ago'
],
[
'field' => 'campaign_count',
'op' => 'opened_at_least',
'value' => [
'count' => 3,
'since' => '90 days ago'
]
]
]
]
]
],
'timezone' => '<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.put("https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"definition\": {\n \"schema_version\": 1,\n \"match\": \"all\",\n \"conditions\": [\n {\n \"field\": \"attributes.country\",\n \"op\": \"equal\",\n \"value\": \"DE\"\n },\n {\n \"field\": \"created_at\",\n \"op\": \"after\",\n \"value\": \"30 days ago\"\n },\n {\n \"field\": \"list\",\n \"op\": \"in\",\n \"value\": \"6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b\"\n },\n {\n \"match\": \"any\",\n \"conditions\": [\n {\n \"field\": \"campaign\",\n \"op\": \"opened\",\n \"value\": \"9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b\"\n },\n {\n \"field\": \"any_campaign\",\n \"op\": \"clicked\",\n \"value\": \"14 days ago\"\n },\n {\n \"field\": \"campaign_count\",\n \"op\": \"opened_at_least\",\n \"value\": {\n \"count\": 3,\n \"since\": \"90 days ago\"\n }\n }\n ]\n }\n ]\n },\n \"timezone\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_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 \"description\": \"<string>\",\n \"definition\": {\n \"schema_version\": 1,\n \"match\": \"all\",\n \"conditions\": [\n {\n \"field\": \"attributes.country\",\n \"op\": \"equal\",\n \"value\": \"DE\"\n },\n {\n \"field\": \"created_at\",\n \"op\": \"after\",\n \"value\": \"30 days ago\"\n },\n {\n \"field\": \"list\",\n \"op\": \"in\",\n \"value\": \"6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b\"\n },\n {\n \"match\": \"any\",\n \"conditions\": [\n {\n \"field\": \"campaign\",\n \"op\": \"opened\",\n \"value\": \"9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b\"\n },\n {\n \"field\": \"any_campaign\",\n \"op\": \"clicked\",\n \"value\": \"14 days ago\"\n },\n {\n \"field\": \"campaign_count\",\n \"op\": \"opened_at_least\",\n \"value\": {\n \"count\": 3,\n \"since\": \"90 days ago\"\n }\n }\n ]\n }\n ]\n },\n \"timezone\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"object": "segment",
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"created_at": "2023-11-07T05:31:56Z",
"updated_at": "2023-11-07T05:31:56Z",
"name": "<string>",
"description": "<string>",
"definition": {
"schema_version": 1,
"match": "all",
"conditions": [
{
"field": "attributes.country",
"op": "equal",
"value": "DE"
},
{
"field": "created_at",
"op": "after",
"value": "30 days ago"
},
{
"field": "list",
"op": "in",
"value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b"
},
{
"match": "any",
"conditions": [
{
"field": "campaign",
"op": "opened",
"value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b"
},
{
"field": "any_campaign",
"op": "clicked",
"value": "14 days ago"
},
{
"field": "campaign_count",
"op": "opened_at_least",
"value": {
"count": 3,
"since": "90 days ago"
}
}
]
}
]
},
"timezone": "<string>",
"state": "building",
"failure_reason": "<string>",
"member_count": 1,
"last_evaluated_at": "2023-11-07T05:31:56Z"
}{
"message": "conditions[0].field: the account has no contact field \"city\"; conditions[1].value: must be a list id"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}segments:write.
Fields:
- A field that you leave out or send as
nulldoes not change. nameanddescriptionfollow the same rules as in Create Segment.definitionreplaces the stored rules as a whole. See Definitions.timezoneis an IANA time zone. An empty string storesUTC.- A field that you cannot write is refused with
400, and the message names it.
- A change of
definitionortimezonebuilds the members again. The segment isbuildinguntil that ends, and a campaign that uses it cannot be sent until then. - A change of only
nameordescriptionkeeps the members and the state. - Any update of a
failedsegment,{}included, builds it again.
409 with The segment is being updated. Try again in a minute. Nothing was changed, so send the request again later.
Related Guides
- API authentication, Permission scopes, Get Segment.
- Segments API overview, API errors, API rate limits.
Authorizations
API key for authentication. Non-empty Security Requirement values are AhaSend API-key roles. Roles listed within one requirement object are jointly required; separate requirement objects are alternatives.
Body
A partial segment update. An omitted or null property leaves the
stored value unchanged. A change of definition or timezone builds
the segment's members again, and it is building until that ends; a
change of only name or description leaves its members and its
state as they are. A property that is not writable is rejected with
400 naming it.
Trimmed and bounded exactly as on create.
1 - 255Trimmed and bounded exactly as on create.
1000The new rules, which replace the stored ones as a whole.
Show child attributes
Show child attributes
{ "schema_version": 1, "match": "all", "conditions": [ { "field": "attributes.country", "op": "equal", "value": "DE" }, { "field": "created_at", "op": "after", "value": "30 days ago" }, { "field": "list", "op": "in", "value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b" }, { "match": "any", "conditions": [ { "field": "campaign", "op": "opened", "value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b" }, { "field": "any_campaign", "op": "clicked", "value": "14 days ago" }, { "field": "campaign_count", "op": "opened_at_least", "value": { "count": 3, "since": "90 days ago" } } ] } ] }
The IANA time zone, as on create. Null and omission keep the stored zone; an empty string stores UTC.
Response
Segment updated
A saved set of rules over the account's contacts, and the contacts that match them.
Object type identifier
segment Unique identifier for the segment
When the segment was created
When the segment was last saved
Segment name; names are not unique within an account
Free-text description; empty when none was given
The rules of a segment. The root group holds conditions and groups;
a group holds conditions only, so groups nest one level deep. A
definition has at most 50 conditions in all, and at most 5 of them
are campaign, any_campaign or campaign_count conditions. An
unknown key, or a schema_version other than 1, is refused. A
refused definition is answered with 400 and a message that names each
problem by its path, such as conditions[3].value.
Show child attributes
Show child attributes
{ "schema_version": 1, "match": "all", "conditions": [ { "field": "attributes.country", "op": "equal", "value": "DE" }, { "field": "created_at", "op": "after", "value": "30 days ago" }, { "field": "list", "op": "in", "value": "6f1c2a52-8f2e-4d8e-9a3b-1c2d3e4f5a6b" }, { "match": "any", "conditions": [ { "field": "campaign", "op": "opened", "value": "9a2e7c41-0b3d-4f5e-8a6b-7c8d9e0f1a2b" }, { "field": "any_campaign", "op": "clicked", "value": "14 days ago" }, { "field": "campaign_count", "op": "opened_at_least", "value": { "count": 3, "since": "90 days ago" } } ] } ] }
The IANA time zone that relative times and days in the definition are read in
building: the members are being found for the current definition. A campaign that uses the segment cannot be sent until it isready.ready: the members follow the current definition.failed: the members could not be found;failure_reasonsays why. Any update of the segment,{}included, builds it again.
building, ready, failed Why the segment failed. Null unless state is failed.
The contacts that match the rules, whatever their consent. A
campaign reaches only those of them who may get marketing email;
Get Segment counts them as marketable_count.
x >= 0When the members were last checked against the whole account. Null until the first build ends.

