1
votes

I want to create a new subscription from my backend nodejs solution

I followed this https://docs.microsoft.com/en-us/rest/api/servicebus/create-subscription

But I can not complete it I do not understand how this REST API works and I think Im missing the authorization.

In postman this:

PUT https://management.core.windows.net/{subscription ID}/services/ServiceBus/Namespaces/Topics/Subscriptions/{Subscription Name}

Returns: ForbiddenError The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

2
what are the headers that you are sending along with the request?Yash Kumar Verma
@YashKumarVerma Content-Type:application/xml Namespace:https://{namespace}.servicebus.windows.net/{topic} x-ms-version:2012-03-01Faisal Taibi
you can use the package @azure/arm-servicebus to create it : npmjs.com/package/@azure/arm-servicebusJim Xu
@FaisalTaibi Do you have any update?Jim Xu
Took me quite some time to find the documentation on thisLiam

2 Answers

1
votes

The latest version 7.0.0 of @azure/service-bus offers ServiceBusAdministrationClient that lets you manage the service-bus resources.

For the problem in this question, you can leverage the ServiceBusAdministrationClient.createSubscription method to create a subscription for a topic. A similar method would be present in the @azure/arm-service-bus library too.

Here is an example on using ServiceBusAdministrationClient - administrationClient.ts

Refer to the links below for more info on version 7.0.0 @azure/service-bus.

1
votes

The easiest way is to use @azure/service-bus:

https://www.npmjs.com/package/@azure/service-bus

NodeJS

const { ServiceBusAdministrationClient } = require('@azure/service-bus');
            
// Define connection string and related Service Bus entity names here
const connectionString =
          'Endpoint=sb://<REST OF CONNECTION STRING>';
const topicName = 'yourTopic',
const subscriptionName = 'yourNewSubscription'
        
const serviceBusAdministrationClient = new ServiceBusAdministrationClient(connectionString);

serviceBusAdministrationClient.createSubscription('yourTopic', 'yourNewSubscription').catch((err) => {
          console.log(err);
        });

If you want create filters. Ex with correlation filter:

serviceBusAdministrationClient.createRule(
    'yourTopic',
    'yourNewSubscription',
    'CorrelationFilter', //this is the name of the filter
    { correlationId: 'aCorrelationId' }
  );