You can publish a CloudEvents event directly to an Eventarc Advanced
bus in a supported format using the Google Cloud CLI or by sending a request to
the Eventarc Publishing REST API. You can also use the
[Eventarc client libraries](https://docs.cloud.google.com/eventarc/docs/reference/libraries.md.txt)
to access the Eventarc APIs from a supported language.

## Before you begin

Before you start, make sure that you have completed the following steps:

<br />

## Publish directly

The event message you publish directly must conform to the
[CloudEvents specification](https://docs.cloud.google.com/eventarc/advanced/docs/event-format.md.txt).

### gcloud

1. Open a terminal.

2. You can publish events to a bus by using the
   [`gcloud eventarc message-buses publish`](https://docs.cloud.google.com/sdk/gcloud/reference/eventarc/message-buses/publish)
   command. For example:

   ```bash
   gcloud eventarc message-buses publish BUS_NAME \
       --avro-message=AVRO_MESSAGE
   ```
   Or:

   ```bash
   gcloud eventarc message-buses publish BUS_NAME \
       --json-message=JSON_MESSAGE
   ```
   Or:

   ```bash
   gcloud eventarc message-buses publish BUS_NAME \
       --event-data=DATA_PAYLOAD \
       --event-id=EVENT_ID \
       --event-source=EVENT_SOURCE \
       --event-type=EVENT_TYPE \
       --event-attributes=EVENT_ATTRIBUTE
   ```

   Replace the following:
   - `BUS_NAME`: the ID or fully qualified identifier of the bus to which to publish the event.

   You must use only *one* of the following:
   - `AVRO_MESSAGE`: the event message in an Avro format according to this [specification](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/formats/avro-format.md).
   - `JSON_MESSAGE`: the event message in a JSON format according to this [specification](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/formats/json-format.md).
   - `DATA_PAYLOAD`: the data of a published event.

   If using the `--event-data` flag, you must also use the following:
   - `EVENT_ID`: the event identifier. Event producers must ensure that `source` + `id` is unique for each distinct event.
   - `EVENT_SOURCE`: the event source of a published event.
   - `EVENT_TYPE`: the type of event related to the originating occurrence.

   If using the `--event-data` flag, you can optionally use the following:
   - `EVENT_ATTRIBUTE`: the attributes of a published event.
     You can repeat the `--event-attributes` flag to add more attributes.

     Note that events might include any number of additional custom
     CloudEvents attributes with distinct names (also known as
     [extension attributes](https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#extension-context-attributes)).

### Examples:

```bash
gcloud eventarc message-buses publish my-bus \
    --event-data='{"key": "hello-world-data"}' \
    --event-id=hello-world-id-1234 \
    --event-source=hello-world-source \
    --event-type=hello-world-type \
    --event-attributes="datacontenttype=application/json"
```

<br />

```bash
gcloud eventarc message-buses publish my-bus --json-message='{
    "specversion" : "1.0",
    "type" :"com.example.someevent",
    "source" : "google.cloud.storage.object.v1.finalized",
    "id" : "A234-1234-1234",
    "time" : "2024-04-05T17:31:00Z",
    "bucket" : "bucketName",
    "datacontenttype" : "application/json",
    "data":{"key": "value"}
}'
```

### REST API

To publish an event to a bus, use the
[`projects.locations.messageBuses.publish`](https://docs.cloud.google.com/eventarc/docs/reference/publishing/rest/v1/projects.locations.messageBuses/publish.md.txt)
method.

> [!NOTE]
> **Note:** In the `ProtoMessage` of the request body, the `protoData` field only functions as expected when the payload is specifically a `CloudEvent` message type, and can't be used for arbitrary protocol buffer messages. For any other protocol buffer type, you must serialize your proto message into bytes, and use the `binaryData` field instead.


Before using any of the request data,
make the following replacements:

- The full resource name of the bus in the format `projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME`

  Replace the following:
  - `PROJECT_ID`: the Google Cloud project ID for the bus project.
  - `LOCATION`: the [region](https://docs.cloud.google.com/eventarc/docs/locations.md.txt) in which the bus is deployed---for example, `us-central1`.
  - `BUS_NAME`: the name of the bus to which to publish the event.
- `SPEC_VERSION`: the version of the CloudEvents specification which the event uses---for example, `1.0`.
- `EVENT_TYPE`: the type of event related to the originating occurrence.
- `EVENT_SOURCE`: the event source of a published event.
- `EVENT_ID`: the event identifier. Producers must ensure that `source` + `id` is unique for each distinct event.
- `CONTENT_TYPE` (optional): the content type of `data` value. If a JSON format event has no `datacontenttype` attribute, then it is assumed that the data is a JSON value conforming to the `application/json` media type.
- `DATA_PAYLOAD` (optional): the event payload encoded into the media format specified by `datacontenttype` and adhering to `dataschema` when those attributes are present.


Request JSON body:

```
{
"jsonMessage":
  "{\"specversion\":\"SPEC_VERSION\",
  \"type\":\"EVENT_TYPE\",
  \"source\":\"EVENT_SOURCE\",
  \"id\":\"EVENT_ID\",
  \"datacontenttype\":\"CONTENT_TYPE\",
  \"data\":\"DATA_PAYLOAD\"}"
}
```

To send your request, expand one of these options:

#### curl (Linux, macOS, or Cloud Shell)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`.
Run the following command in the terminal to create or overwrite
this file in the current directory:

```
cat > request.json << 'EOF'
{
"jsonMessage":
  "{\"specversion\":\"SPEC_VERSION\",
  \"type\":\"EVENT_TYPE\",
  \"source\":\"EVENT_SOURCE\",
  \"id\":\"EVENT_ID\",
  \"datacontenttype\":\"CONTENT_TYPE\",
  \"data\":\"DATA_PAYLOAD\"}"
}
EOF
```


Then execute the following command to send your REST request:

```
curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d @request.json \
     "https://eventarcpublishing.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME:publish"
```

#### PowerShell (Windows)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`.
Run the following command in the terminal to create or overwrite
this file in the current directory:

```
@'
{
"jsonMessage":
  "{\"specversion\":\"SPEC_VERSION\",
  \"type\":\"EVENT_TYPE\",
  \"source\":\"EVENT_SOURCE\",
  \"id\":\"EVENT_ID\",
  \"datacontenttype\":\"CONTENT_TYPE\",
  \"data\":\"DATA_PAYLOAD\"}"
}
'@  | Out-File -FilePath request.json -Encoding utf8
```


Then execute the following command to send your REST request:

```
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://eventarcpublishing.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME:publish" | Select-Object -Expand Content
```

#### APIs Explorer (browser)


Copy the request body and open the

[method reference page](https://docs.cloud.google.com/eventarc/docs/reference/publishing/rest/v1/projects.locations.messageBuses/publish.md.txt).
The APIs Explorer panel opens on the right side of the page.
You can interact with this tool to send requests.

Paste the request body in this tool, complete any other required fields, and click **Execute**.


If successful, the server returns an HTTP `200 OK` status code and the empty response
body in JSON format:

```
200 OK

{}
```

<br />

## What's next

- [Publish events from Google sources](https://docs.cloud.google.com/eventarc/advanced/docs/publish-events/publish-events-google-sources.md.txt)
- [Transform received events](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/transform-events.md.txt)
- [Format received events](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/configure-format-events.md.txt)