# GCP Pub/Sub

Publish events to a Google Cloud Pub/Sub topic.

## Creating a Pub/Sub Destination

```sh
curl 'https://api.outpost.hookdeck.com/2025-07-01/tenants/<TENANT_ID>/destinations' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API_KEY>' \
--data '{
  "type": "gcp_pubsub",
  "topics": ["orders"],
  "config": {
    "project_id": "my-gcp-project",
    "topic": "my-pubsub-topic"
  },
  "credentials": {
    "service_account_json": "{\"type\": \"service_account\", ...}"
  }
}'

```

## Configuration

### Config

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `config.project_id` | string | Yes | GCP project ID |
| `config.topic` | string | Yes | Pub/Sub topic name |
| `config.endpoint` | string | No | Custom endpoint for the Pub/Sub emulator |

### Credentials

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `credentials.service_account_json` | string | Yes* | Service account JSON key file contents |

*Not required when using the Pub/Sub emulator.

## Message Format

Events are published as Pub/Sub messages:

* Data: The event's `data` field as JSON
* Attributes: System metadata (`event-id`, `topic`, `timestamp`) plus any event metadata from the published event

### Example

Publishing this event:

```json
{
  "topic": "orders",
  "data": { "order_id": "123", "status": "created" },
  "metadata": { "source": "checkout-service" }
}

```

Results in a Pub/Sub message with:

Data:

```json
{"order_id": "123", "status": "created"}

```

Attributes:

| Attribute | Value |
| --- | --- |
| `event-id` | `evt_123` |
| `topic` | `orders` |
| `timestamp` | `1704067200` |
| `source` | `checkout-service` |

## Creating a Service Account

1. In the GCP Console, navigate to IAM & Admin > Service Accounts
2. Click Create Service Account
3. Grant the Pub/Sub Publisher role (`roles/pubsub.publisher`)
4. Create a JSON key and download it
5. Use the JSON file contents as `credentials.service_account_json`

## Local Development with the Pub/Sub Emulator

```json
{
  "type": "gcp_pubsub",
  "topics": ["orders"],
  "config": {
    "project_id": "test-project",
    "topic": "test-topic",
    "endpoint": "localhost:8085"
  }
}

```

When using the emulator, `service_account_json` is not required.