Ruby gem for Novu Api's

Ruby gem for Novu Api's

ยท

3 min read

Introduction ๐Ÿ‘‹

Novu is an open-source notification infrastructure designed to help engineering teams build rich product notification experiences without constantly reinventing the wheel.

One such library is the Novu client library, which is used for communicating with the Novu API. The Novu API is a powerful tool that allows you to manage notifications and other data in your Novu account.

Why Use Novu? ๐Ÿ‘€

Developing a notification layer is a common challenge when designing new applications. The process usually starts with sending simple emails, but as the application grows, managing hundreds of notifications across multiple channels becomes cumbersome. Novu's goal is to assist developers in creating meaningful, transactional communication between the product and its users, all while providing an easy-to-use API and outstanding developer experience.

Unified API Today, users receive communication across various providers such as Sendgrid for Email, Twilio for SMS, Mailchimp, Push, Web-Push, and Chat messaging (Slack, etc.). Users expect customization of communication channels to fit their needs and goals. Consequently, developers must manage all of those APIs across the codebase.

Novu solves this challenge by providing a unified API to manage all customer communication. With Novu, developers can add a new provider or switch email providers seamlessly.

Installation ๐Ÿง‘๐Ÿผโ€๐Ÿ’ป

To install the Novu client library, you can use RubyGems. Simply run the following command in your terminal:

gem install novu

Alternatively, you can add the library to your Gemfile and run the bundle install.

gem 'novu'

Usage โšก๏ธ

Once you have installed the gem, you can start using it in your Ruby code. The first step is to initialize the client with your API token:

require 'novu'
client = Novu::Client.new('MY_API_TOKEN')

Replace MY_API_TOKEN with your actual API token. You can find your API token from settings by visiting the Api keys option.

Note: You have to signup onNovuto get the API key from their dashboard.

After initializing the client, you can call methods on it to interact with the Novu API. For example:

  • To get a list of notifications, you can use the following code:
client.notifications

This will return an array of notification objects. You can also pass parameters to this method to filter the results.

Similarly, to trigger an event you can use the following code:

The trigger event is the main (and the only) way to send notifications to subscribers. The trigger identifier is used to match the particular template associated with it.

client = Novu::Client.new('MY_API_TOKEN')

body = {
  name: '<notification_template_id>',
  payload: {
    customVariables: 'Hello'
  },
  overrides: {
    fcm: {
      color: '#fff'
    }
  },
  to: {
    subscriberId: '<USER_IDENTIFIER>',
    email: 'amansaini0003@gmail.com',
    firstName: 'Aman',
    lastName: 'Saini',
  },
  transactionId: 'transactionId'
}.to_json

# trigger event
client.trigger_event(body)
# HTTP Status Code: 201
# Response Body:
{
    acknowledged: true,
    status: "status",
    error: ["error"],
    transactionId: "transactionId"
}

For more information about the available methods and their parameters, you can refer to the Novu API documentation.

Conclusion ๐Ÿš€

Novu is an open-source notification infrastructure designed to assist engineering teams in creating meaningful, transactional communication between the product and its users, all while providing an easy-to-use API and an outstanding developer experience. By using Novu, developers can simplify the management of multiple communication channels and providers, improving efficiency and productivity. By following the installation and usage instructions provided above, you can start using the library in your Ruby projects right away.

Cheers,
Aman Saini

PS. If you find the article relevant, then please share it with your network. ๐Ÿ™

References

ย