This document describes how to create a push subscription. You can use the
Google Cloud console, the Google Cloud CLI, the client library, or the
Pub/Sub API to create a push subscription.

## Before you begin

- Learn about [subscriptions](https://docs.cloud.google.com/pubsub/docs/subscription-overview.md.txt).
- Understand how [push subscriptions](https://docs.cloud.google.com/pubsub/docs/push.md.txt) work.

### Required roles and permissions


To get the permissions that
you need to create a push subscription,

ask your administrator to grant you the
[Pub/Sub Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/pubsub.md.txt#pubsub.editor) (`roles/pubsub.editor`) IAM role on the project.


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


This predefined role contains

the permissions required to create a push subscription. To see the exact permissions that are
required, expand the **Required permissions** section:


#### Required permissions

The following permissions are required to create a push subscription:

- `pubsub.subscriptions.create` on the project
- `pubsub.topics.attachSubscription` on the topic


You might also be able to get
these permissions
with [custom roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles.md.txt) or
other [predefined roles](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#predefined).

### Cross-project subscriptions

If you create a subscription in one project for a topic in another project, you
must have `pubsub.subscriptions.create` permission on the project in which you
are creating the subscription, and `pubsub.topics.attachSubscription` permission
on the topic.

## Push subscription properties

Push subscriptions support all of the
[common subscription properties](https://docs.cloud.google.com/pubsub/docs/subscription-properties.md.txt). The
following sections describe properties that are specific to push subscriptions.

### Endpoints

**Endpoint URL (required).** A publicly accessible HTTPS address. The server for the push
endpoint must have a valid SSL certificate signed by a certificate authority.
The Pub/Sub service delivers messages to push endpoints from
the same Google Cloud region that the Pub/Sub service
stores the messages. The Pub/Sub service delivers messages from
the same Google Cloud region on a best-effort basis.

- If subscribers use a firewall, they can't receive push requests. To receive
  push requests, you must turn off the firewall and [verify the JSON Web Token
  (JWT) used in the request](https://docs.cloud.google.com/pubsub/docs/authenticate-push-subscriptions).
  If a subscriber has a firewall, you might receive a `403 permission denied`
  error.

- Pub/Sub doesn't require proof of ownership for push
  subscription URL domains. If your domain receives unexpected POST requests
  from Pub/Sub, you can [report suspected
  abuse](https://support.google.com/code/contact/cloud_platform_report).

### Authentication

**Enable authentication.** When enabled, messages delivered by Pub/Sub to the push endpoint include an authorization header to
allow the endpoint to authenticate the request. Automatic authentication and
authorization mechanisms are available for App Engine Standard and Cloud Run functions endpoints hosted in the same project as the subscription.

The [authentication configuration](https://docs.cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions.md.txt#oidctoken)
for an authenticated push subscription consists of a user-managed service account,
and the audience parameters that
are specified in a [create](https://docs.cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/create.md.txt),
[patch](https://docs.cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/patch.md.txt), or
[ModifyPushConfig](https://docs.cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/modifyPushConfig.md.txt)
call. You must also grant a specific role to a service account, as discussed in
the next section.

- **Audience.** A single, case-insensitive string that the webhook
  uses to validate the intended audience of this particular token.

- **Service account.** Pub/Sub automatically creates a
  service account for you with the format
  `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com`.

#### Prerequisites for enabling authentication

The *user-managed service account* is the service account associated
with the push subscription. This account is used as the `email` claim of the
generated JSON Web Token (JWT). The following is a list of requirements for
the service account:

- This *user-managed service account* must be in the same project as the
  push subscription.

- The principal who is creating or modifying the push subscription must
  have the `iam.serviceAccounts.actAs` permission on the *user-managed
  service account* in order to attach the service account to the push
  subscription. For more information, see
  [Attaching service accounts to resources](https://docs.cloud.google.com/iam/docs/service-account-permissions.md.txt#attaching).

- **Required permissions** : This service account must be granted the
  `iam.serviceAccounts.getOpenIdToken` permission (included in the
  [`roles/iam.serviceAccountTokenCreator`](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountTokenCreator)role) to allow Pub/Sub
  to create [JWT tokens](https://docs.cloud.google.com/pubsub/docs/authenticate-push-subscriptions.md.txt#jwt_format)
  for the specified service account to authenticate push requests.

### Payload unwrapping

The **Enable payload unwrapping** option strips Pub/Sub
messages of all message metadata, except for the message data. With payload
unwrapping, the message data is delivered directly as the HTTP body.

You can also Enable the **Write metadata** option. The **Write metadata** option
adds previously removed message metadata back into the request header.

## Deliver to private VPC addresses

Pub/Sub operates outside of VPC networks and cannot directly push
messages to private VPC addresses. However, you can use Eventarc
to route messages to services within your VPC. Pub/Sub can push
messages to an Eventarc trigger, which can then forward them to a
service within your VPC, such as a Cloud Run
service or a Workflows execution. For more information, see the [Eventarc
documentation](https://docs.cloud.google.com/eventarc/standard/docs/vpc-endpoints/receive-vpc-internal-endpoint).

## VPC Service Controls

For a project protected by
[VPC Service Controls](https://docs.cloud.google.com/vpc-service-controls/docs/overview.md.txt),
note the following limitations for push subscriptions:

- You can only create new push subscriptions for which the push endpoint is
  set to a Cloud Run service with a default `run.app` URL or a
  Workflows execution. Custom domains don't work.

- When routing events through Eventarc to Workflows
  destinations for which the push endpoint is set to a Workflows
  execution, you can only create new push subscriptions through Eventarc.

- You can't update existing push subscriptions. These push subscriptions
  continue to function, although they are not protected by VPC Service Controls.

## Create a push subscription

The following samples demonstrate how to create a subscription with push
delivery, using the provided default settings.

By default, subscriptions use
[pull delivery](https://docs.cloud.google.com/pubsub/docs/pull.md.txt),
unless you explicitly set a push configuration, as shown in the following examples.

### Console


To create a push subscription, follow these steps:

1. In the Google Cloud console, go to the **Subscriptions** page.


   [Go to Subscriptions](https://console.cloud.google.com/cloudpubsub/subscription)

   <br />

2. Click **Create subscription**.

3. In the **Subscription ID** field, enter a name.

   For information on how to name a subscription, see
   [Guidelines to name a topic or a subscription](https://docs.cloud.google.com/pubsub/docs/pubsub-basics.md.txt#resource_names).
4. In the **Pub/Sub topic** list, select a topic for the subscription to
   read from.

5. For **Delivery type** , select **Push**.

6. In the **Endpoint URL** field, enter the endpoint URL.

7. Optional: To enable authentication, follow these steps:

   1. Select **Enable authentication**.
   2. In the **Service account** list, select the service account to perform authentication.
   3. Optional: In the **Audience** field, enter an audience.

   For more information, see
   [Authentication for push subscriptions](https://docs.cloud.google.com/pubsub/docs/authenticate-push-subscriptions.md.txt).
8. Optional: To enable [Payload unwrapping](https://docs.cloud.google.com/pubsub/docs/payload-unwrapping.md.txt),
   select **Enable payload unwrapping**.

   To preserve message metadata in the request header, also select
   **Write metadata** . This option also sets a `Content-Type` header for your
   messages.
9. Optional: In the **Transforms** section, add one or more
   [Single Message Transforms](https://docs.cloud.google.com/pubsub/docs/smts/smts-overview.md.txt) (SMTs). For
   more information, see
   [Create a subscription with SMTs](https://docs.cloud.google.com/pubsub/docs/smts/create-subscription-smt.md.txt).

10. Optional: In the **Filter** field, enter a filter expression to filter
    messages from the subscription. For more information, see
    [Filter messages from a subscription](https://docs.cloud.google.com/pubsub/docs/subscription-message-filter.md.txt).

11. For **Retry policy** , select an option. For more information, see
    [Subscription retry policy](https://docs.cloud.google.com/pubsub/docs/subscription-retry-policy.md.txt).

12. Optional: Enable a [dead-letter topic](https://docs.cloud.google.com/pubsub/docs/dead-letter-topics.md.txt) to
    receive undeliverable messages.

    1. Select the **Dead lettering** checkbox.

    2. In the **Dead letter topic** list, select or create the dead-letter
       topic.

    3. In the **Maximum delivery attempts** field, enter the maximum number
       of delivery attempts.

13. Optional: In the **Delivery properties** section, enable or disable the
    following delivery options:

    - [Exactly-once delivery](https://docs.cloud.google.com/pubsub/docs/exactly-once-delivery.md.txt)

    - [Message ordering](https://docs.cloud.google.com/pubsub/docs/ordering.md.txt)

14. Optional: In the **Acknowledgement deadline** section, set the deadline
    for the subscriber to process and acknowledge messages. For more
    information, see
    [Extend ack time with lease management](https://docs.cloud.google.com/pubsub/docs/lease-management.md.txt).

15. Optional: In the **Lifetime options** section, configure how long the
    subscription retains messages.

    1. In the **Message retention duration** section, specify how long
       unacknowledged messages are retained.

    2. To retain *acknowledged* messages as well as unacknowledged messages,
       select the **Retain acknowledged messages** checkbox.

    For more information, see
    [Configure message retention for a subscription](https://docs.cloud.google.com/pubsub/docs/subscription-message-retention.md.txt).
16. Optional: For **Expiration period**, select an option:

    1. To set the subscription expiration, select the
       **Expire after this many days of inactivity** checkbox. Enter the
       number of days that the subscription can remain inactive before
       Pub/Sub deletes the subscription.

    2. To disable subscription expiry, select the **Never expire** checkbox.

    For more information, see
    [Subscription expiry](https://docs.cloud.google.com/pubsub/docs/subscription-overview.md.txt#lifecycle).
17. Click **Create**.

You can also create a subscription from the **Topics** section.
This shortcut is useful for associating topics with subscriptions.

1. In the Google Cloud console, go to the **Topics** page.


   [Go to Topics](https://console.cloud.google.com/cloudpubsub/topic)

   <br />

2. Click next to the topic on which to create a
   subscription.

3. From the context menu, select **Create subscription**.

4. In the **Add subscription to topic** page, complete the steps described
   in the previous procedure. The topic ID is automatically filled in.

### gcloud


1. In the Google Cloud console, activate Cloud Shell.

   [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)


   At the bottom of the Google Cloud console, a
   [Cloud Shell](https://docs.cloud.google.com/shell/docs/how-cloud-shell-works.md.txt)
   session starts and displays a command-line prompt. Cloud Shell is a shell environment
   with the Google Cloud CLI
   already installed and with values already set for
   your current project. It can take a few seconds for the session to initialize.
2. To create a push subscription, run the [`gcloud pubsub subscriptions create`](https://docs.cloud.google.com/sdk/gcloud/reference/pubsub/subscriptions/create) command.

   ```bash
   gcloud pubsub subscriptions create SUBSCRIPTION_ID \
   --topic=TOPIC_ID \
   --push-endpoint=PUSH_ENDPOINT
   ```

   Replace the following:
   - `SUBSCRIPTION_ID`: The name or ID of your new push subscription.
   - `TOPIC_ID`: The name or ID of your topic.
   - <var translate="no">PUSH_ENDPOINT</var>: the URL to use as the endpoint for this subscription. For example, `https://myproject.appspot.com/myhandler`.

<br />

### REST


To create a push subscription, use the
[`projects.subscriptions.create`](https://docs.cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/create.md.txt)
method:

Request:

The request must be authenticated with an access token in the `Authorization` header. To obtain an access token for the current Application Default Credentials: [gcloud auth application-default print-access-token](https://docs.cloud.google.com/sdk/gcloud/reference/auth/application-default/print-access-token.md.txt).

```
PUT https://pubsub.googleapis.com/v1/projects/PROJECT_ID/subscriptions/SUBSCRIPTION_ID
Authorization: Bearer ACCESS_TOKEN
```

Request body:

```restructuredtext
{
"topic": "projects/PROJECT_ID/topics/TOPIC_ID",
// Only needed if you are using push delivery
"pushConfig": {
"pushEndpoint": "PUSH_ENDPOINT"
}
}
```

Where:
- <var translate="no">PROJECT_ID</var> is your project ID.
- <var translate="no">SUBSCRIPTION_ID</var> is your subscription ID.
- <var translate="no">TOPIC_ID</var> is your topic ID.
- <var translate="no">PUSH_ENDPOINT</var> is a URL to use as the endpoint. For example, `https://myproject.appspot.com/myhandler`.
- Response:

```restructuredtext
{
"name": "projects/PROJECT_ID/subscriptions/SUBSCRIPTION_ID",
"topic": "projects/PROJECT_ID/topics/TOPIC_ID",
"pushConfig": {
"pushEndpoint": "https://PROJECT_ID.appspot.com/myhandler",
"attributes": {
  "x-goog-version": "v1"
}
},
"ackDeadlineSeconds": 10,
"messageRetentionDuration": "604800s",
"expirationPolicy": {
"ttl": "2678400s"
}
}
```

### C++


Before trying this sample, follow the C++ setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub C++ API reference documentation](https://googleapis.dev/cpp/google-cloud-pubsub/latest/).

```c++
namespace pubsub = ::google::cloud::pubsub;
namespace pubsub_admin = ::google::cloud::pubsub_admin;
[](pubsub_admin::SubscriptionAdminClient client,
   std::string const& project_id, std::string const& topic_id,
   std::string const& subscription_id, std::string const& endpoint) {
  google::pubsub::v1::Subscription request;
  request.set_name(
      pubsub::Subscription(project_id, subscription_id).FullName());
  request.set_topic(pubsub::Topic(project_id, topic_id).FullName());
  request.mutable_push_config()->set_push_endpoint(endpoint);
  auto sub = client.CreateSubscription(request);
  if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
    std::cout << "The subscription already exists\n";
    return;
  }
  if (!sub) throw std::move(sub).status();

  std::cout << "The subscription was successfully created: "
            << sub->DebugString() << "\n";
```

<br />

### C#


Before trying this sample, follow the C# setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub C# API reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.PubSub.V1/latest).

```c#

using Google.Cloud.PubSub.V1;

public class CreatePushSubscriptionSample
{
    public Subscription CreatePushSubscription(string projectId, string topicId, string subscriptionId, string pushEndpoint)
    {
        SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create();
        TopicName topicName = TopicName.FromProjectTopic(projectId, topicId);
        SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);

        PushConfig pushConfig = new PushConfig { PushEndpoint = pushEndpoint };

        // The approximate amount of time in seconds (on a best-effort basis) Pub/Sub waits for the
        // subscriber to acknowledge receipt before resending the message.
        var ackDeadlineSeconds = 60;
        var subscription = subscriber.CreateSubscription(subscriptionName, topicName, pushConfig, ackDeadlineSeconds);
        return subscription;
    }
}
```

<br />

### Go


The following sample uses the major version of the Go Pub/Sub client library (v2). If you are still using the v1 library, see
[the migration guide to v2](https://github.com/googleapis/google-cloud-go/blob/main/pubsub/MIGRATING.md).
To see a list of v1 code samples, see [the deprecated code samples](https://docs.cloud.google.com/pubsub/docs/samples.md.txt?language=golang&text=deprecated).

Before trying this sample, follow the Go setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub Go API reference documentation](https://pkg.go.dev/cloud.google.com/go/pubsub/v2).

```go
import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsub/v2"
	"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func createWithEndpoint(w io.Writer, projectID, topic, subscription, endpoint string) error {
	// projectID := "my-project-id"
	// topic := "projects/my-project-id/topics/my-topic"
	// subscription := "projects/my-project/subscriptions/my-sub"
	// endpoint := "https://my-test-project.appspot.com/push"
	ctx := context.Background()
	client, err := pubsub.NewClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("pubsub.NewClient: %w", err)
	}
	defer client.Close()

	sub, err := client.SubscriptionAdminClient.CreateSubscription(ctx, &pubsubpb.Subscription{
		Name:               subscription,
		Topic:              topic,
		AckDeadlineSeconds: 10,
		PushConfig:         &pubsubpb.PushConfig{PushEndpoint: endpoint},
	})
	if err != nil {
		return fmt.Errorf("failed to create push sub: %w", err)
	}
	fmt.Fprintf(w, "Created push subscription: %v\n", sub)
	return nil
}
```

<br />

### Java


Before trying this sample, follow the Java setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub Java API reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-pubsub/latest/overview).

```java

import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.PushConfig;
import com.google.pubsub.v1.Subscription;
import com.google.pubsub.v1.SubscriptionName;
import com.google.pubsub.v1.TopicName;
import java.io.IOException;

public class CreatePushSubscriptionExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String subscriptionId = "your-subscription-id";
    String topicId = "your-topic-id";
    String pushEndpoint = "https://my-test-project.appspot.com/push";

    createPushSubscriptionExample(projectId, subscriptionId, topicId, pushEndpoint);
  }

  public static void createPushSubscriptionExample(
      String projectId, String subscriptionId, String topicId, String pushEndpoint)
      throws IOException {
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
      TopicName topicName = TopicName.of(projectId, topicId);
      SubscriptionName subscriptionName = SubscriptionName.of(projectId, subscriptionId);
      PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(pushEndpoint).build();

      // Create a push subscription with default acknowledgement deadline of 10 seconds.
      // Messages not successfully acknowledged within 10 seconds will get resent by the server.
      Subscription subscription =
          subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, 10);
      System.out.println("Created push subscription: " + subscription.getName());
    }
  }
}
```

<br />

### Node.js


Before trying this sample, follow the Node.js setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub Node.js API reference documentation](https://googleapis.dev/nodejs/pubsub/latest).

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const pushEndpoint = 'YOUR_ENDPOINT_URL';
    // const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
    // const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';

    // Imports the Google Cloud client library
    const {PubSub} = require('@google-cloud/pubsub');

    // Creates a client; cache this for further use
    const pubSubClient = new PubSub();

    async function createPushSubscription(
      pushEndpoint,
      topicNameOrId,
      subscriptionNameOrId,
    ) {
      const options = {
        pushConfig: {
          // Set to an HTTPS endpoint of your choice. If necessary, register
          // (authorize) the domain on which the server is hosted.
          pushEndpoint,
        },
      };

      await pubSubClient
        .topic(topicNameOrId)
        .createSubscription(subscriptionNameOrId, options);
      console.log(`Subscription ${subscriptionNameOrId} created.`);
    }

<br />

### Node.ts


Before trying this sample, follow the Node.js setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub Node.js API reference documentation](https://googleapis.dev/nodejs/pubsub/latest).

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const pushEndpoint = 'YOUR_ENDPOINT_URL';
    // const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
    // const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';

    // Imports the Google Cloud client library
    import {PubSub, CreateSubscriptionOptions} from '@google-cloud/pubsub';

    // Creates a client; cache this for further use
    const pubSubClient = new PubSub();

    async function createPushSubscription(
      pushEndpoint: string,
      topicNameOrId: string,
      subscriptionNameOrId: string,
    ) {
      const options: CreateSubscriptionOptions = {
        pushConfig: {
          // Set to an HTTPS endpoint of your choice. If necessary, register
          // (authorize) the domain on which the server is hosted.
          pushEndpoint,
        },
      };

      await pubSubClient
        .topic(topicNameOrId)
        .createSubscription(subscriptionNameOrId, options);
      console.log(`Subscription ${subscriptionNameOrId} created.`);
    }

<br />

### PHP


Before trying this sample, follow the PHP setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub PHP API reference documentation](https://cloud.google.com/php/docs/reference/cloud-pubsub/latest).

```php
use Google\Cloud\PubSub\PubSubClient;

/**
 * Creates a Pub/Sub push subscription.
 *
 * @param string $projectId  The Google project ID.
 * @param string $topicName  The Pub/Sub topic name.
 * @param string $subscriptionName  The Pub/Sub subscription name.
 * @param string $endpoint  The endpoint for the push subscription.
 */
function create_push_subscription($projectId, $topicName, $subscriptionName, $endpoint)
{
    $pubsub = new PubSubClient([
        'projectId' => $projectId,
    ]);
    $topic = $pubsub->topic($topicName);
    $subscription = $topic->subscription($subscriptionName);
    $subscription->create([
        'pushConfig' => ['pushEndpoint' => $endpoint]
    ]);

    printf('Subscription created: %s' . PHP_EOL, $subscription->name());
}
```

<br />

### Python


Before trying this sample, follow the Python setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub Python API reference documentation](https://docs.cloud.google.com/python/docs/reference/pubsub/latest.md.txt).

```python
from google.cloud import pubsub_v1

# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
# endpoint = "https://my-test-project.appspot.com/push"

publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)

push_config = pubsub_v1.types.PushConfig(push_endpoint=endpoint)

# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
    subscription = subscriber.create_subscription(
        request={
            "name": subscription_path,
            "topic": topic_path,
            "push_config": push_config,
        }
    )

print(f"Push subscription created: {subscription}.")
print(f"Endpoint for subscription is: {endpoint}")
```

<br />

### Ruby


The following sample uses Ruby Pub/Sub client library v3. If you are still using the v2 library, see
[the migration guide to v3](https://github.com/googleapis/google-cloud-ruby/blob/main/google-cloud-pubsub/MIGRATION_GUIDE.md).
To see a list of Ruby v2 code samples, see [the deprecated code samples](https://docs.cloud.google.com/pubsub/docs/samples.md.txt?language=ruby&text=deprecated).

Before trying this sample, follow the Ruby setup instructions in
[Quickstart: Using Client Libraries](https://docs.cloud.google.com/pubsub/docs/create-topic-client-libraries).
For more information, see the [Pub/Sub Ruby API reference documentation](https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html).

```ruby
# topic_id          = "your-topic-id"
# subscription_id   = "your-subscription-id"
# endpoint          = "https://your-test-project.appspot.com/push"

pubsub = Google::Cloud::PubSub.new
subscription_admin = pubsub.subscription_admin

subscription = subscription_admin.create_subscription \
  name: pubsub.subscription_path(subscription_id),
  topic: pubsub.topic_path(topic_id),
  push_config: {
    push_endpoint: endpoint
  }

puts "Push subscription #{subscription_id} created."
```

<br />

## Monitor push subscriptions

Cloud Monitoring provides a number of metrics to
[monitor subscriptions](https://docs.cloud.google.com/pubsub/docs/monitoring.md.txt#maintain_a_healthy_subscription).

For a list of all the available metrics related to Pub/Sub
and their descriptions, see the
[Monitoring documentation for Pub/Sub](https://docs.cloud.google.com/monitoring/api/metrics_gcp_p_z.md.txt#gcp-pubsub).

You can also monitor subscriptions from [within Pub/Sub](https://docs.cloud.google.com/pubsub/docs/monitor-subscription.md.txt).

## What's next

- Create or modify a subscription with [`gcloud` commands](https://docs.cloud.google.com/sdk/gcloud/reference/pubsub/subscriptions).
- Create or modify a subscription with [REST APIs](https://docs.cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions.md.txt).