Skip to content
Skip to main content

Google Contacts API PHP example

Last updated:

Nylas doesn’t require a PHP SDK to read Google Contacts. You can call the REST API directly from PHP with the connected user’s grant ID and your server-side API key.

This is useful when you want a lightweight Google Contacts integration without wiring up the Google People API client, OAuth token refresh, and provider-specific contact schema yourself.

You’ll need:

  • A Nylas application with a valid API key
  • A Google grant created through hosted OAuth
  • Contact scopes on the Google connector, usually https://www.googleapis.com/auth/contacts.readonly
  • PHP with cURL enabled

Store NYLAS_API_KEY in your server environment. Don’t put it in a browser bundle, template, or public repository.

Send a GET request to /v3/grants/{grant_id}/contacts. The example below reads saved contacts from the Google address book and prints the first email address for each contact.

Use source=address_book for contacts the user saved in Google Contacts. Use source=inbox for automatically created contacts from Gmail correspondents, but only if your connector includes the contacts.other.readonly scope.

The Contacts API returns contacts in pages. Set limit=200 for bulk sync, then pass next_cursor back as page_token until no cursor remains.

For a customer relationship management import or address-book sync, fetch all pages first, then remove duplicate records by lowercase email before writing to your database.

Use the email query parameter when you already know the address and need the contact record behind it.

The API doesn’t have fuzzy name search. If you need a search box, list contacts, cache them, and then filter given_name, surname, and emails in PHP or JavaScript. See Search contacts for the exact filter behavior.

Things to know about PHP Google Contacts integrations

Section titled “Things to know about PHP Google Contacts integrations”

Use OAuth once, then store the grant ID. Your PHP app doesn’t need to store Google access tokens. Nylas refreshes provider tokens for the grant.

Escape output. Contact names and notes come from a user’s address book. Treat them as data from outside your app and escape before rendering HTML.

Handle empty fields. Many Google contacts have no company, title, phone number, or name. Build your display around email as the most reliable identifier.

Watch rate limits on bulk imports. Use limit=200, page through results, and avoid one API request per rendered row.