> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailglyph.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Contacts

> Create, list, update, count, and delete contacts with the MailGlyph Go SDK.

Use a **secret key** client for contacts methods.

```go theme={null}
import (
  "context"
  "os"

  mailglyph "github.com/MailGlyph/mailglyph-go"
)

client := mailglyph.New(os.Getenv("MAILGLYPH_SECRET_KEY"))
ctx := context.Background()
```

## List contacts (`Contacts.List`)

Use cursor pagination for large lists:

```go theme={null}
limit := 50
subscribed := true
search := "john"

page1, err := client.Contacts.List(ctx, &mailglyph.ListContactsParams{
  Limit: &limit,
  Subscribed: &subscribed,
  Search: &search,
})
if err != nil {
  panic(err)
}

println(len(page1.Data), page1.HasMore)
```

Fetch the next page with `Cursor`:

```go theme={null}
if page1.HasMore && page1.Cursor != nil {
  page2, err := client.Contacts.List(ctx, &mailglyph.ListContactsParams{
    Limit: &limit,
    Cursor: page1.Cursor,
  })
  if err != nil {
    panic(err)
  }

  println(len(page2.Data))
}
```

## Create a contact (`Contacts.Create`)

```go theme={null}
subscribed := true

created, err := client.Contacts.Create(ctx, &mailglyph.CreateContactParams{
  Email: "user@example.com",
  Subscribed: &subscribed,
  Data: map[string]interface{}{
    "firstName": "John",
    "plan": "premium",
  },
})
if err != nil {
  panic(err)
}

println(created.ID, created.Meta.IsNew, created.Meta.IsUpdate)
```

## Get one contact (`Contacts.Get`)

```go theme={null}
contact, err := client.Contacts.Get(ctx, created.ID)
if err != nil {
  panic(err)
}

println(contact.Email, contact.Subscribed)
```

## Update a contact (`Contacts.Update`)

### Update subscription status

```go theme={null}
unsubscribed := false

updated, err := client.Contacts.Update(ctx, created.ID, &mailglyph.UpdateContactParams{
  Subscribed: &unsubscribed,
})
if err != nil {
  panic(err)
}

println(updated.Subscribed)
```

### Update custom data

```go theme={null}
updated, err := client.Contacts.Update(ctx, created.ID, &mailglyph.UpdateContactParams{
  Data: map[string]interface{}{
    "plan": "pro",
  },
})
if err != nil {
  panic(err)
}

println(updated.Data["plan"] == "pro")
```

## Count contacts (`Contacts.Count`)

```go theme={null}
count, err := client.Contacts.Count(ctx, nil)
if err != nil {
  panic(err)
}

subscribedOnly := true
subscribedCount, err := client.Contacts.Count(ctx, &mailglyph.ListContactsParams{
  Subscribed: &subscribedOnly,
})
if err != nil {
  panic(err)
}

println(count, subscribedCount)
```

## Delete a contact (`Contacts.Delete`)

```go theme={null}
if err := client.Contacts.Delete(ctx, created.ID); err != nil {
  panic(err)
}
```

See full details in the [Contacts API reference](https://docs.mailglyph.com/api-reference/contacts/listContacts.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.