AhaSend Go SDK
package main
import (
"context"
"fmt"
"log"
"os"
"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()
// Pass the raw email; the SDK encodes it once in the path.
response, _, err := client.SegmentsAPI.GetContactSegments(ctx, accountID, "User+Tag@Example.COM", requests.GetContactSegmentsParams{})
if err != nil {
log.Fatalf("Error getting the contact's segments: %v", err)
}
for _, segment := range response.Data {
fmt.Println(segment.Name)
}
}import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
const idOrEmail = "User+Tag@Example.COM";
const page = await client.contacts.segments.list(idOrEmail, { limit: 20 });
console.log("Contact segments listed.", { count: page.data.length });
curl --request GET \
--url https://api.ahasend.com/v2/accounts/{account_id}/contacts/{id_or_email}/segments \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/contacts/{id_or_email}/segments"
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}/contacts/{id_or_email}/segments",
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}/contacts/{id_or_email}/segments")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/contacts/{id_or_email}/segments")
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": "list",
"data": [
{
"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"
}
],
"pagination": {
"has_more": true,
"next_cursor": "<string>",
"previous_cursor": "<string>"
}
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}Segments
List a Contact's Segments via the API
List the AhaSend segments that one contact is a member of, newest segment first, to see which rules a person matches and which campaigns can reach them.
GET
/
v2
/
accounts
/
{account_id}
/
contacts
/
{id_or_email}
/
segments
AhaSend Go SDK
package main
import (
"context"
"fmt"
"log"
"os"
"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()
// Pass the raw email; the SDK encodes it once in the path.
response, _, err := client.SegmentsAPI.GetContactSegments(ctx, accountID, "User+Tag@Example.COM", requests.GetContactSegmentsParams{})
if err != nil {
log.Fatalf("Error getting the contact's segments: %v", err)
}
for _, segment := range response.Data {
fmt.Println(segment.Name)
}
}import { AhaSendClient } from "@ahasend/sdk";
const client = AhaSendClient.fromEnv();
const idOrEmail = "User+Tag@Example.COM";
const page = await client.contacts.segments.list(idOrEmail, { limit: 20 });
console.log("Contact segments listed.", { count: page.data.length });
curl --request GET \
--url https://api.ahasend.com/v2/accounts/{account_id}/contacts/{id_or_email}/segments \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.ahasend.com/v2/accounts/{account_id}/contacts/{id_or_email}/segments"
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}/contacts/{id_or_email}/segments",
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}/contacts/{id_or_email}/segments")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.ahasend.com/v2/accounts/{account_id}/contacts/{id_or_email}/segments")
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": "list",
"data": [
{
"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"
}
],
"pagination": {
"has_more": true,
"next_cursor": "<string>",
"previous_cursor": "<string>"
}
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}{
"message": "Error message"
}The AhaSend API returns the segments that one contact is a member of, newest first. Access requires
segments:read and contacts:read, because the request reads a contact.
{id_or_email} is the contact ID or its email address. Percent-encode an email address exactly once. See Name a contact in the path.
A segment that is building may still list the contact by its previous rules. See States. This is the reverse of List Segment Contacts. To read the lists of the contact, use List Contact’s Lists.
A contact that does not exist answers 404.
Pages use cursors: send after or before, not both. See Pagination.
Related Guides
- API authentication, Permission scopes, Get Contact.
- 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.
Path Parameters
Account ID
Contact UUID or email address. Percent-encode an email exactly once as a path segment; SDK callers pass the raw email value.
Query Parameters
Maximum number of segments to return
Required range:
1 <= x <= 100Cursor for the next page; cannot be combined with before
Cursor for the previous page; cannot be combined with after

