package main
import (
"context"
"fmt"
"log"
"os"
"github.com/AhaSend/ahasend-go/api"
"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")
response, _, err := client.SegmentsAPI.GetSegment(ctx, accountID, segmentID)
if err != nil {
log.Fatalf("Error getting segment: %v", err)
}
// MarketableCount is nil while the segment is not ready.
if response.MarketableCount != nil {
fmt.Printf("%s: %d members, %d a campaign would reach\n", response.Name, response.MemberCount, *response.MarketableCount)
} else {
fmt.Printf("%s: %d members (%s)\n", response.Name, response.MemberCount, response.State)
}
}
import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
const segmentId = "00000000-0000-4000-8000-000000000015";
const segment = await client.segments.get(segmentId);
// marketable_count is null while the segment is not ready.
console.log("Segment found.", {
id: segment.id,
state: segment.state,
marketableCount: segment.marketable_count,
});
curl --request GET \
--url https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id} \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, 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 => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.get("https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}")
.header("Authorization", "Bearer <token>")
.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::Get.new(url)
request["Authorization"] = 'Bearer <token>'
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",
"marketable_count": 1
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}Get a Segment via the API
Retrieve one AhaSend segment by ID with its rules, state and member count, and marketable_count, the members that a campaign to the segment would reach.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/AhaSend/ahasend-go/api"
"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")
response, _, err := client.SegmentsAPI.GetSegment(ctx, accountID, segmentID)
if err != nil {
log.Fatalf("Error getting segment: %v", err)
}
// MarketableCount is nil while the segment is not ready.
if response.MarketableCount != nil {
fmt.Printf("%s: %d members, %d a campaign would reach\n", response.Name, response.MemberCount, *response.MarketableCount)
} else {
fmt.Printf("%s: %d members (%s)\n", response.Name, response.MemberCount, response.State)
}
}
import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
const segmentId = "00000000-0000-4000-8000-000000000015";
const segment = await client.segments.get(segmentId);
// marketable_count is null while the segment is not ready.
console.log("Segment found.", {
id: segment.id,
state: segment.state,
marketableCount: segment.marketable_count,
});
curl --request GET \
--url https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id} \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, 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 => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.get("https://api.ahasend.com/v2/accounts/{account_id}/segments/{segment_id}")
.header("Authorization", "Bearer <token>")
.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::Get.new(url)
request["Authorization"] = 'Bearer <token>'
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",
"marketable_count": 1
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}segments:read.
member_countis the number of contacts that match the rules, whatever their consent.marketable_countis the number of members that a campaign to this segment alone would reach. See Members and reachable contacts for what it counts. It isnullwhile the segment is notready, and when the members cannot be counted within 10 seconds.stateisbuilding,readyorfailed. Afailedsegment gives the reason infailure_reason. See States.last_evaluated_atis the time the members were last checked against the whole account. It isnulluntil the first build ends.
404.
Related Guides
- API authentication, Permission scopes, Update 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.
Response
The segment
A segment as Get Segment returns it, with marketable_count.
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.
The members a campaign to this segment alone would reach:
enabled, not unsubscribed, not found invalid, and either
confirmed on at least one list or on no list at all. A
contact who unsubscribed from or complained on every list
they were on, or has not confirmed a double opt-in, is not
counted. Null while state is not ready, since no campaign
sends to the segment then, and null when they cannot be
counted within 10 seconds.
x >= 0
