Skip to content

Contacts

Contacts

Contacts(auth: GoogleAuth, person_fields: str = PERSON_FIELDS)

High-level Google Contacts client (People API).

Example

auth = GoogleAuth(scopes=Scopes.contacts()) auth.authenticate()

contacts = Contacts(auth)

for contact in contacts.search("ana"): print(contact.display_name, contact.email)

contact = contacts.create(given_name="Ana", emails=["ana@example.com"]) contacts.update(contact.id, phones=["+54 11 5555-5555"])

Initialize the client.

Parameters:

Name Type Description Default
auth GoogleAuth

GoogleAuth with the contacts scope

required
person_fields str

People API fields to read (comma-separated); add e.g. "addresses,birthdays" and find them in Contact.raw

PERSON_FIELDS

iter_contacts

iter_contacts(max_results: int | None = None, sort_order: SortOrder | None = None) -> Iterator[Contact]

Iterate over the user's contacts, following pagination.

list_contacts

list_contacts(max_results: int | None = None, **kwargs: Any) -> list[Contact]

The user's contacts (see iter_contacts).

search

search(query: str, max_results: int = 10) -> list[Contact]

Contacts whose names, emails, phones or organizations start with query.

The People API serves searches from a cache that a first, empty query warms up; without it the first results can be stale or empty. The client sends that warm-up once.

sync

sync(sync_token: str | None = None) -> ContactsDelta

Contacts changed or deleted since sync_token (all contacts without one).

Keep delta.sync_token for the next call. The first sync lists every contact; later ones return only what changed, deleted contacts included (Contact.deleted).

Raises:

Type Description
SyncTokenExpiredError

The token is too old (about 7 days) or was issued for other person fields; sync again without it.

get_many

get_many(contact_ids: list[str]) -> list[Contact]

Several contacts by ID, 200 per request; missing ones are left out.

get

get(contact_id: str) -> Contact | None

A contact by ID ("c123" or "people/c123"), or None if it doesn't exist.

create

create(given_name: str | None = None, family_name: str | None = None, emails: list[str] | None = None, phones: list[str] | None = None, organization: str | None = None, job_title: str | None = None, notes: str | None = None, raw_fields: dict[str, Any] | None = None) -> Contact

Create a contact. Needs at least a name, an email or a phone.

Parameters:

Name Type Description Default
given_name str | None

First name

None
family_name str | None

Last name

None
emails list[str] | None

Email addresses, primary first

None
phones list[str] | None

Phone numbers, primary first

None
organization str | None

Company

None
job_title str | None

Title at the company

None
notes str | None

Free text ("biographies" in the API)

None
raw_fields dict[str, Any] | None

More Person fields as the API takes them, e.g. {"urls": [{"value": "https://..."}], "birthdays": [...]}

None

update

update(contact_id: str, given_name: str | None = None, family_name: str | None = None, emails: list[str] | None = None, phones: list[str] | None = None, organization: str | None = None, job_title: str | None = None, notes: str | None = None, raw_fields: dict[str, Any] | None = None) -> Contact

Change fields of a contact; fields left as None are kept.

emails and phones replace the whole list ([] clears it). Changing the given name keeps the family name and vice versa, and the same for organization and job title. raw_fields sets any other Person field as the API takes it (each replaces that whole field), e.g. {"urls": [{"value": "https://..."}]}.

Raises:

Type Description
NotFoundError

The contact doesn't exist.

update_raw

update_raw(contact_id: str, body: dict[str, Any], update_fields: str | list[str]) -> Contact

Send a People API Person body as it is, without reading the contact first.

For callers that build the body themselves: body must carry the contact's etag (a stale one fails instead of overwriting a newer change) and update_fields names the fields it replaces.

delete

delete(contact_id: str) -> bool

Delete a contact. False if it didn't exist.

list_groups

list_groups() -> list[ContactGroup]

The user's contact groups (labels), system groups included.

create_group

create_group(name: str) -> ContactGroup

Create a contact group (label).

delete_group

delete_group(group_id: str, delete_contacts: bool = False) -> bool

Delete a group; its contacts stay unless delete_contacts. False if missing.

group_members

group_members(group_id: str) -> list[str]

Resource names ("people/c...") of the group's contacts, all of them.

modify_group

modify_group(group_id: str, add: list[str] | None = None, remove: list[str] | None = None) -> list[str]

Add contacts to a group and remove others (IDs or resource names).

Returns:

Type Description
list[str]

Contacts Google didn't find, or couldn't remove because the group

list[str]

is their last one; the rest were changed

AsyncContacts

AsyncContacts(auth: Any, person_fields: str = PERSON_FIELDS, *, timeout: float | None = None, transport: Any = None)

Async Google Contacts client (People API).

Parameters:

Name Type Description Default
auth Any

GoogleAuth with the contacts scope, or google-auth credentials

required
person_fields str

People API fields to read (see Contacts)

PERSON_FIELDS
timeout float | None

Seconds per request (default: GSUITE_REQUEST_TIMEOUT)

None
transport Any

httpx transport (tests)

None

aclose async

aclose() -> None

Close the HTTP connection pool.

iter_contacts async

iter_contacts(max_results: int | None = None, sort_order: SortOrder | None = None) -> AsyncIterator[Contact]

Yield the user's contacts, following pagination.

list_contacts async

list_contacts(max_results: int | None = None, sort_order: SortOrder | None = None) -> list[Contact]

The user's contacts.

search async

search(query: str, max_results: int = 10) -> list[Contact]

Contacts whose names, emails, phones or organizations start with query.

Sends the People API's cache warm-up (an empty query) once, as Contacts does.

get async

get(contact_id: str) -> Contact | None

A contact by ID ("c123" or "people/c123"), or None if it doesn't exist.

get_many async

get_many(contact_ids: list[str]) -> list[Contact]

Several contacts by ID, 200 per request; missing ones are left out.

sync async

sync(sync_token: str | None = None) -> ContactsDelta

Contacts changed or deleted since sync_token (see Contacts.sync).

Raises:

Type Description
SyncTokenExpiredError

The token is too old; sync again without it.

create async

create(given_name: str | None = None, family_name: str | None = None, emails: list[str] | None = None, phones: list[str] | None = None, organization: str | None = None, job_title: str | None = None, notes: str | None = None, raw_fields: dict[str, Any] | None = None) -> Contact

Create a contact (same arguments as Contacts.create).

update async

update(contact_id: str, given_name: str | None = None, family_name: str | None = None, emails: list[str] | None = None, phones: list[str] | None = None, organization: str | None = None, job_title: str | None = None, notes: str | None = None, raw_fields: dict[str, Any] | None = None) -> Contact

Change fields of a contact (same arguments and rules as Contacts.update).

Raises:

Type Description
NotFoundError

The contact doesn't exist.

update_raw async

update_raw(contact_id: str, body: dict[str, Any], update_fields: str | list[str]) -> Contact

Send a People API Person body as it is (see Contacts.update_raw).

delete async

delete(contact_id: str) -> bool

Delete a contact; False if it didn't exist.

list_groups async

list_groups() -> list[ContactGroup]

The user's contact groups (labels), system groups included.

create_group async

create_group(name: str) -> ContactGroup

Create a contact group (label).

delete_group async

delete_group(group_id: str, delete_contacts: bool = False) -> bool

Delete a group; its contacts stay unless delete_contacts. False if missing.

group_members async

group_members(group_id: str) -> list[str]

Resource names of every contact in the group.

modify_group async

modify_group(group_id: str, add: list[str] | None = None, remove: list[str] | None = None) -> list[str]

Add and remove contacts; returns those Google left as they were.

Contact dataclass

Contact(resource_name: str, etag: str | None = None, display_name: str | None = None, given_name: str | None = None, family_name: str | None = None, emails: list[str] = list(), phones: list[str] = list(), organization: str | None = None, job_title: str | None = None, notes: str | None = None, urls: list[str] = list(), groups: list[str] = list(), deleted: bool = False, raw: dict[str, Any] = dict())

A contact of the user (a People API Person in the "myContacts" group).

Only the primary value of single-valued fields is kept; emails and phones keep every value, primary first. raw has the full resource.

id property

id: str

The ID without the "people/" prefix.

email property

email: str | None

Primary email.

phone property

phone: str | None

Primary phone number.

ContactGroup dataclass

ContactGroup(resource_name: str, name: str, group_type: str = 'USER_CONTACT_GROUP', member_count: int = 0, etag: str | None = None, raw: dict[str, Any] = dict())

A contact group: one of the user's labels, or a system group like "myContacts".

id property

id: str

The ID without the "contactGroups/" prefix.

is_system property

is_system: bool

myContacts, starred and the like: they can't be renamed or deleted.

ContactsDelta dataclass

ContactsDelta(changed: list[Contact] = list(), deleted: list[Contact] = list(), sync_token: str | None = None)

What changed since a sync token (everything, on the first sync).

Keep sync_token and pass it to the next Contacts.sync.