You're viewing **Apigee Edge** documentation.  
[Go to the
**Apigee X** documentation](http://cloud.google.com/apigee/docs). info


> [!NOTE]
> **Note:** Apigee Extensions are available in the Edge UI to Google Cloud Apigee customers with an Enterprise [support](https://cloud.google.com/apigee/pricing) plan.

> [!NOTE]
> **Note:** Extensions marked as deprecated will no longer be available for selection by users. However, the currently deployed version(s) will remain unaffected.

> [!CAUTION]
> **Caution:** This version of the extension is no longer supported and has been deprecated.

<br />

> Version: 1.5.1

Publish messages to a topic in the Google Cloud Pub/Sub service.

This content provides reference for configuring and using this extension.

## Prerequisites

Before using the extension from an API proxy using the [ExtensionCallout policy](https://docs.apigee.com/api-platform/reference/policies/extension-callout-policy), you must:

1. Create a Cloud Pub/Sub topic, as described in [Quickstart: Using the console](https://cloud.google.com/pubsub/docs/quickstart-console).

2. Once you have the topic, [grant permission](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts) to publish to your Cloud Pub/Sub topic. For example, you can use the `roles/pubsub.publisher` role to publish to topics. For more on Cloud Pub/Sub roles, see [Access Control](https://cloud.google.com/pubsub/docs/access-control).

3. [Use the GCP Console to generate a key for the service account](https://cloud.google.com/iam/docs/creating-managing-service-account-keys).

4. Use the contents of the resulting key JSON file when [adding and configuring](https://docs.apigee.com/api-platform/extensions/configuring-an-extension) the extension using the [configuration reference](https://docs.apigee.com/api-platform/reference/extensions/google-cloud-pub-sub/google-cloud-pub-sub-extension-151#configuration_reference).

## About Cloud Pub/Sub

[Cloud Pub/Sub](https://cloud.google.com/pubsub/docs/overview) provides many-to-many, asynchronous messaging that decouples senders and receivers. Cloud Pub/Sub delivers low-latency, durable messaging that helps developers quickly integrate systems hosted on the Google Cloud Platform and externally.

If you're just getting started with Cloud Pub/Sub, take a look at the [quickstarts in the Cloud Pub/Sub documentation](https://cloud.google.com/pubsub/docs/quickstarts).

## Samples

The following examples illustrate how to configure support for Cloud Pub/Sub extension actions using the [ExtensionCallout policy](https://docs.apigee.com/api-platform/reference/policies/extension-callout-policy).

### Text message

In this example, the `publish` action publishes a message as a simple string to a topic specified in the extension's [configuration](https://docs.apigee.com/api-platform/reference/extensions/google-cloud-pub-sub/google-cloud-pub-sub-extension-151#configuration_reference). The `publish` action returns the ID of the published message in the topic to which it was published and writes it to the variable specified by the `<Output>` tag:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ConnectorCallout async="false" continueOnError="true" enabled="true" name="Publish-Message">
        <DisplayName>Publish Message</DisplayName>
        <Connector>cloud-pub-sub-extension-example</Connector>
        <Action>publish</Action>
        <Input><![CDATA[
          {
              "message" : "My test message."
          }
        ]]></Input>
        <Output>topic.message</Output>
    </ConnectorCallout>

The following [Assign Message](https://docs.apigee.com/api-platform/reference/policies/assign-message-policy) policy uses the value of the variable storing the extension's response to assign the response payload. The message ID is stored in the `messageId` property of the output variable:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <AssignMessage async="false" continueOnError="false" enabled="true" name="Get-Topic-Message-ID">
        <DisplayName>Get Topic Message ID</DisplayName>
        <AssignTo type="response" createNew="false"/>
        <Set>
            <Payload contentType="application/json">{topic.message.messageId}</Payload>
        </Set>
    </AssignMessage>

The response data is returned as JSON:

    {"messageId":"165918654986954"}

Therefore, the value of `topic.message.messageId` is 165918654986954.

### JSON message

In this example, the `publish` action publishes a message as JSON to a topic specified in the extension's [configuration](https://docs.apigee.com/api-platform/reference/extensions/google-cloud-pub-sub/google-cloud-pub-sub-extension-151#configuration_reference). The `publish` action returns the ID of the published message in the topic to which it was published and writes it to the variable specified by the `<Output>` tag:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ConnectorCallout async="false" continueOnError="true" enabled="true" name="Publish-Message">
        <DisplayName>Publish Message</DisplayName>
        <Connector>cloud-pub-sub-extension-example</Connector>
        <Action>publish</Action>
        <Input><![CDATA[
          {
              "message" : { "info" : "My test message" }
          }
        ]]></Input>
        <Output>topic.message</Output>
    </ConnectorCallout>

The following [Assign Message](https://docs.apigee.com/api-platform/reference/policies/assign-message-policy) policy uses the value of the variable storing the extension's response to assign the response payload. The message ID is stored in the `messageId` property of the output variable:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <AssignMessage async="false" continueOnError="false" enabled="true" name="Get-Topic-Message-ID">
        <DisplayName>Get Topic Message ID</DisplayName>
        <AssignTo type="response" createNew="false"/>
        <Set>
            <Payload contentType="application/json">{topic.message.messageId}</Payload>
        </Set>
    </AssignMessage>

The response data is returned as JSON:

    {"messageId":"165918654986954"}

Therefore, the value of `topic.message.messageId` is 165918654986954.

## Actions


> [!NOTE]
> **Note:** See the [ExtensionCallout policy](https://docs.apigee.com/api-platform/reference/policies/extension-callout-policy) for more information on setting [`<Action>`](https://docs.apigee.com/api-platform/reference/policies/extension-callout-policy#action_element), [`<Input>`](https://docs.apigee.com/api-platform/reference/policies/extension-callout-policy#input_element), and [`<Output>`](https://docs.apigee.com/api-platform/reference/policies/extension-callout-policy#output_element).

<br />

### publish

Publishes a message to the topic specified in this extension's [configuration](https://docs.apigee.com/api-platform/reference/extensions/google-cloud-pub-sub/google-cloud-pub-sub-extension-151#configuration_reference).

### Syntax

    <Action>publish</Action>
    <Input><![CDATA[{
      "message" : "message-to-publish" | JSON-structured-data,
      "customAttributes": JSON-array
    }]]></Input>

### String Example

    <Action>publish</Action>
    <Input><![CDATA[
      {
          "message" : "My test message."
      }
    ]]></Input>

### JSON Structured Data Example

    <Action>publish</Action>
    <Input><![CDATA[
      {
          "message" : { "info" : "My test message" }
      }
    ]]></Input>

### Custom Attributes Example

    <Action>publish</Action>
    <Input><![CDATA[
      {
          "message" : { "info" : "My test message" },
          "customAttributes": {"attr1": "foo", "attr2": "bar"}
      }
    ]]></Input>

#### Request parameters

| Parameter | Description | Type | Default | Required |
|---|---|---|---|---|
| message | The message to publish. You can use a simple string or structure your message with JSON of your own design. | String or JSON | None | Yes |
| customAttributes | Custom attributes to add to the message in the form: `"customAttributes": {"attr1": "foo", "attr2": "bar"}`. | JSON array | None | No |

#### Response

JSON containing the ID of the message as published to the topic. For example:

    {"messageId":"165918654986954"}

## Configuration Reference

Use the following when you're configuring and deploying this extension for use in API proxies.

> [!NOTE]
> **Note:** The ability to configure and deploy extensions is available only to organization administrators.

### Common extension properties

The following properties are present for every extension.

| Property | Description | Default | Required |
|---|---|---|---|
| `name` | Name you're giving this configuration of the extension. | None | Yes |
| `packageName` | Name of the extension package as given by Apigee Edge. | None | Yes |
| `version` | Version number for the extension package from which you're configuring an extension. | None | Yes |
| `configuration` | Configuration value specific to the extension you're adding. See [Properties for this extension package](https://docs.apigee.com/api-platform/reference/extensions/google-cloud-pub-sub/google-cloud-pub-sub-extension-151#properties_for_this_extension_package) | None | Yes |

### Properties for this extension package

| Property | Description | Default | Required |
|---|---|---|---|
| `topic` | The topic to publish to. Get this value from the Pub/Sub service, such as from the GCP console for the service. For example, the topic name might take the form `projects/GCP-PROJECT-ID/topics/TOPIC-NAME` | None. | Yes. |
| `credentials` | When entered in the Apigee Edge console, this is the content of your [service account key file](https://cloud.google.com/iam/docs/creating-managing-service-account-keys). When sent via the management API, it is a base64-encoded value generated from the service account key file. | None. | Yes. |