> For the complete documentation index, see [llms.txt](https://docs.sprinthub.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sprinthub.com/en/topics/integrations/integration-google-ads-and-gmail/connect-gmail-in-real-time-pub-sub.md).

# Connect Gmail in real time (Pub/Sub)

This guide shows, **screen by screen**, how to configure Google Cloud Pub/Sub so SprintHub can receive Gmail emails in **real time** in SAC360 (seconds), instead of waiting for periodic synchronization via IMAP (1–5 min).

> The screens were captured by going through the real process from scratch (new project). The project/topic/account names used here are examples — replace them with your own.

**Summary of what will be created in Google Cloud:**

| Resource            | Example in this guide                                                  |
| ------------------- | ---------------------------------------------------------------------- |
| Project             | `sprinthub-gmail-pubsub`                                               |
| Service account     | `sprinthub-pubsub-push@sprinthub-gmail-pubsub.iam.gserviceaccount.com` |
| Pub/Sub topic       | `projects/sprinthub-gmail-pubsub/topics/gmail-notifications`           |
| Subscription (push) | `gmail-notifications-sub`                                              |

In the end, two values are taken to SprintHub: the **topic name** and the **webhook URL** (this last one is already provided on the integration configuration screen).

***

### Prerequisites

* Google account with access to the [Google Cloud Console](https://console.cloud.google.com/).
* Credit card registered with Google Cloud (Google requirement; usage stays within the free tier — in practice, free).
* The **webhook URL** of your SprintHub instance, which appears on the Gmail integration configuration screen itself (something like `https://sprinthub-api-XXXX.sprinthub.app/email/webhook/gmail`). The path `/email/webhook/gmail` is fixed.

***

### Step 1 — Access the console and sign in

Access <https://console.cloud.google.com/> and sign in with your Google account.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FqXnDkdBL55FM0ezdFpft%2F01-console-home.png?alt=media&amp;token=ccdb43a0-0fcd-43ab-ac6e-f1140e29fb0f" alt=""><figcaption></figcaption></figure>

***

### Step 2 — Create (or select) the project

1. Click on the **project selector** at the top (next to "Google Cloud").

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FvkHvKzh7FiDINJi02ZeU%2F02-selecionar-projeto.png?alt=media&amp;token=45475b24-9f8e-4911-bd0d-e4624cfe09ec" alt=""><figcaption></figcaption></figure>

2. Click on **New project**, give it a name (e.g., `sprinthub-gmail-pubsub`) and click **Create**.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2Fjf5NZ23QPScJrME3VAKv%2F03-novo-projeto.png?alt=media&amp;token=7f8a9f73-75ee-4f53-9cae-3fd50ab3c3cd" alt=""><figcaption></figcaption></figure>

3. Wait for creation (\~30s) and **select the newly created project** in the project selector.

> A newly created project may take a few seconds before access is granted (IAM). If "You need additional access" appears, wait a moment and reload.

***

### Step 3 — Create the service account

1. In **search bar** at the top, type `service accounts` and click the result **Service accounts** (IAM and admin).

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FXvtNBekHCcIUQJmmKZy1%2F04-pesquisa-contas-servico.png?alt=media&amp;token=eb3af74d-a869-4547-ba3a-5a290b2d98f9" alt=""><figcaption></figcaption></figure>

2. On the Service accounts page, click **Create service account**.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FLTsUSTOsf45QpGq5qxOk%2F05-contas-servico-lista.png?alt=media&amp;token=975eba62-ea7a-4fd5-a83e-44704b17a895" alt=""><figcaption></figcaption></figure>

3. Fill in the **Service account name** (e.g.: `sprinthub-pubsub-push`) — the ID and email are generated automatically. The permission steps are optional.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2F3LzTkrFQ0B5KBZFbZ1UK%2F06-criar-conta-servico.png?alt=media&amp;token=dd858f49-6d79-45ed-9fbd-105387f25c97" alt=""><figcaption></figcaption></figure>

4. Click on **Create and close**. The account appears in the list.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FMd4vl5g8C6rP3EfwQVRT%2F07-conta-servico-criada.png?alt=media&amp;token=2429a6a4-b8d5-4fbb-90e4-17e88310c17d" alt=""><figcaption></figcaption></figure>

> Save the service account email — it will be selected in the push subscription (Step 6). E.g.: `sprinthub-pubsub-push@sprinthub-gmail-pubsub.iam.gserviceaccount.com`.

***

### Step 4 — Create the Pub/Sub topic

1. In **search bar**, type `pub/sub` and click the product **Pub/Sub**.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FxWnh8RBMZKaVh4MSC1u1%2F08-pesquisa-pubsub.png?alt=media&amp;token=8b2e1233-7f86-442d-9a4d-b4c49eb77a7e" alt=""><figcaption></figcaption></figure>

> On the first visit, the Cloud Pub/Sub API is automatically enabled for the project.

2. In the Topics list, click **Create topic**.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FNluiYMLxc1l3UxFWJJDY%2F09-topicos-lista.png?alt=media&amp;token=d42de962-7b65-4197-b325-1be7ccf4ebc6" alt=""><figcaption></figcaption></figure>

3. Define the **Topic ID** (e.g.: `gmail-notifications`). **No need to change anything else** — leave the checkbox "Add a default subscription" checked (it creates the subscription we'll edit in Step 6). Click **Create**.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FDxuZxPvvHkojaWe5Z84y%2F10-criar-topico.png?alt=media&amp;token=64e3c8c2-a3dd-4b88-9052-5fc8a04b2c41" alt=""><figcaption></figcaption></figure>

4. The topic is created with a default subscription (`gmail-notifications-sub`).

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FH7Uf7CMVnpMjCGjn8vSU%2F11-topico-criado.png?alt=media&amp;token=f6d8d7c4-7974-4df7-97fe-4a41fa6cc4ff" alt=""><figcaption></figcaption></figure>

***

### Step 5 — Authorize Gmail to publish to the topic

Gmail publishes notifications to the topic using a fixed Google service agent. You need to grant it permission.

1. Still on the topic page, in the permissions panel on the right, click **Add principal**.
2. In **New principals**, paste exactly:

   ```
   gmail-api-push@system.gserviceaccount.com
   ```
3. In **Assign roles**, select the role **Editor** (if needed, search for "editor" in the role selector).

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FbqFhvJFPC7UTmBt21Sae%2F12-permissao-principal-editor.png?alt=media&amp;token=9b78641a-a19d-464e-bd68-7b2cddb60ea5" alt=""><figcaption></figcaption></figure>

4. Click on **Save**.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FUniF9PPkT9UgUNpPkhAg%2F13-permissao-salva.png?alt=media&amp;token=dc58777d-4080-4e62-bceb-f2fb0feb63f4" alt=""><figcaption></figcaption></figure>

> Without this step, when enabling real time the error appears `User not authorized to publish to topic`.

***

### Step 6 — Configure the subscription as Push

The subscription is what turns each topic message into an HTTP call to SprintHub. We are going to **edit the default subscription** created along with the topic (if it doesn't exist, click **Create subscription** on the topic).

1. Open the subscription `gmail-notifications-sub` and click on **Edit**.
2. In **Delivery type**, select **Push**.
3. In **Endpoint URL**, paste the webhook URL shown on the integration configuration screen in SprintHub:

   ```
   https://sprinthub-api-XXXX.sprinthub.app/email/webhook/gmail
   ```
4. Check **Enable authentication** and, in **Service account**, select the account created in Step 3 (`sprinthub-pubsub-push@...`). The field **Audience** can be left blank.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FdqUAH3kjTK0DE0OpYiPl%2F14-assinatura-push-config.png?alt=media&amp;token=0b046317-b1e7-4cb8-ab95-00ac2687c303" alt=""><figcaption></figcaption></figure>

5. Click on **Refresh**.

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FB1wN0XGsOtXSxDllvV1x%2F15-assinatura-atualizada.png?alt=media&amp;token=575c87e6-f949-4f68-bf8a-ed615b065e9c" alt=""><figcaption></figcaption></figure>

> The URL must be **valid HTTPS**. Since we use service account authentication, there is no need to verify the domain in Google.

***

### Step 7 — Copy the topic name

Go back to the topic and copy the **Topic name** in full (copy button next to it):

<pre><code><strong>projects/sprinthub-gmail-pubsub/topics/gmail-notifications
</strong></code></pre>

<figure><img src="https://684109703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9FzUKkagBNy5uQ7AtGNa%2Fuploads%2FizbubUpBMJw428ok5Wig%2F16-copiar-nome-topico.png?alt=media&amp;token=3cbd8ac9-bed2-47f4-b681-5e94032440c0" alt=""><figcaption></figcaption></figure>

***

### Step 8 — Paste it into SprintHub and enable real time

In SprintHub, go to **Settings → Integrations → Gmail account → SAC 360**. The real-time configuration is in the section **"Real-time reception (Pub/Sub)"**, at the end of the SAC 360 card (it only appears with "Create tickets automatically" enabled):

1. **Copy the webhook URL** shown there (copy button) — it is the same one used in Step 6.
2. Paste the topic name in the field **Google Pub/Sub topic**.
3. Turn on the toggle **Receive in real time (Pub/Sub)**.

> Screen notes:
>
> * The toggle **only enables** with a valid Pub/Sub Topic format (`projects/<project>/topics/<topic>`).
> * When **saving the integration without a valid topic**, real time is **automatically disabled** (prevents an inconsistent state).

Done — from here on, every new email in the Gmail account generates a ticket/message in SAC360 in seconds, without waiting for IMAP synchronization.

***

### How to verify that it is working

1. Send a test email to the connected Gmail account.
2. In a few seconds, the ticket/message should appear in SAC360 (without refreshing).
3. In the Google Cloud Console: **Pub/Sub → Subscriptions → `gmail-notifications-sub` → Metrics** should show "Published messages" and "Delivered messages" increasing.

To debug from the server side, the backend logs distinguish the arrival path (filter by `email_attendance`):

```
[EmailAttendance] ⇢ ENTRY source=pubsub topic=projects/.../topics/gmail-notifications account=... messageId=...
[EmailAttendance] ⇢ ENTRY source=imap  account=... messageId=...
```

`source=pubsub` = arrived in real time; `source=imap` = arrived via periodic synchronization.
