• Stars
    star
    218
  • Rank 181,805 (Top 4 %)
  • Language
    Ruby
  • License
    Apache License 2.0
  • Created over 13 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vonage REST API client for Ruby. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.

Vonage Server SDK for Ruby

Gem Version Coverage Status codecov

Nexmo is now known as Vonage

This is the Ruby Server SDK for Vonage APIs. To use it you'll need a Vonage account. Sign up for free at vonage.com.

Requirements

Vonage Ruby supports MRI/CRuby (2.5 or newer), JRuby (9.2.x), and Truffleruby.

Installation

To install the Ruby Server SDK using Rubygems:

gem install vonage

Alternatively you can clone the repository:

git clone [email protected]:Vonage/vonage-ruby-sdk.git

Usage

Begin by requiring the Vonage library:

require 'vonage'

Then construct a client object with your key and secret:

client = Vonage::Client.new(api_key: 'YOUR-API-KEY', api_secret: 'YOUR-API-SECRET')

You can now use the client object to call Vonage APIs. For example, to send an SMS:

client.sms.send(from: 'Ruby', to: '447700900000', text: 'Hello world')

For production you can specify the VONAGE_API_KEY and VONAGE_API_SECRET environment variables instead of specifying the key and secret explicitly, keeping your credentials out of source control.

Logging

Use the logger option to specify a logger. For example:

require 'logger'

logger = Logger.new(STDOUT)

client = Vonage::Client.new(logger: logger)

By default the library sets the logger to Rails.logger if it is defined.

To disable logging set the logger to nil.

Overriding the default hosts

To override the default hosts that the SDK uses for HTTP requests, you need to specify the api_host, rest_host or both in the client configuration. For example:

client = Vonage::Client.new(
  api_host: 'api-sg-1.nexmo.com',
  rest_host: 'rest-sg-1.nexmo.com'
)

By default the hosts are set to api.nexmo.com and rest.nexmo.com, respectively.

JWT authentication

To call newer endpoints that support JWT authentication such as the Voice API and Messages API you'll also need to specify the application_id and private_key options. For example:

client = Vonage::Client.new(application_id: application_id, private_key: private_key)

Both arguments should have string values corresponding to the id and private_key values returned in a "create an application" response. These credentials can be stored in a datastore, in environment variables, on disk outside of source control, or in some kind of key management infrastructure.

By default the library generates a short lived JWT per request. To generate a long lived JWT for multiple requests or to specify JWT claims directly use Vonage::JWT.generate and the token option. For example:

claims = {
  application_id: application_id,
  private_key: 'path/to/private.key',
  nbf: 1483315200,
  ttl: 800
}

token = Vonage::JWT.generate(claims)

client = Vonage::Client.new(token: token)

Documentation for the Vonage Ruby JWT generator gem can be found at https://www.rubydoc.info/github/nexmo/nexmo-jwt-ruby. The documentation outlines all the possible parameters you can use to customize and build a token with.

Webhook signatures

To check webhook signatures you'll also need to specify the signature_secret option. For example:

client = Vonage::Client.new
client.config.signature_secret = 'secret'
client.config.signature_method = 'sha512'

if client.signature.check(request.GET)
  # valid signature
else
  # invalid signature
end

Alternatively you can set the VONAGE_SIGNATURE_SECRET environment variable.

Note: you'll need to contact [email protected] to enable message signing on your account.

Pagination

Vonage APIs paginate list requests. This means that if a collection is requested that is larger than the API default, the API will return the first page of items in the collection. The Ruby SDK provides an auto_advance parameter that will traverse through the pages and return all the results in one response object.

The auto_advance parameter is set to a default of true for the following APIs:

To modify the auto_advance behavior you can specify it in your method:

client.applications.list(auto_advance: false)

NCCO Builder

The Vonage Voice API accepts instructions via JSON objects called NCCOs. Each NCCO can be made up multiple actions that are executed in the order they are written. The Vonage API Developer Portal contains an NCCO Reference with instructions and information on all the parameters possible.

The SDK includes an NCCO builder that you can use to build NCCOs for your Voice API methods.

For example, to build talk and input NCCO actions and then combine them into a single NCCO you would do the following:

talk = Vonage::Voice::Ncco.talk(text: 'Hello World!')
input = Vonage::Voice::Ncco.input(type: ['dtmf'], dtmf: { bargeIn: true })
ncco = Vonage::Voice::Ncco.build(talk, input)

# => [{:action=>"talk", :text=>"Hello World!"}, {:action=>"input", :type=>["dtmf"], :dtmf=>{:bargeIn=>true}}]

Once you have the constructed NCCO you can then use it in a Voice API request:

response = client.voice.create({
  to: [{type: 'phone', number: '14843331234'}],
  from: {type: 'phone', number: '14843335555'},
  ncco: ncco
})

Messages API

The Vonage Messages API allows you to send messages over a number of different channels, and various message types within each channel. See the Vonage Developer Documentation for a complete API reference listing all the channel and message type combinations.

The Ruby SDK allows you to construct message data for specific messaging channels. Other than SMS (which has only one type -- text), you need to pass the message :type as well as the :message itself as arguments to the appropriate messages method, along with any optional properties if needed.

# creating an SMS message
message = Vonage::Messaging::Message.sms(message: 'Hello world!')

# creating a WhatsApp Text message
message = Vonage::Messaging::Message.whatsapp(type: 'text', message: 'Hello world!')

# creating a WhatsApp Image message
message = Vonage::Messaging::Message.whatsapp(type: 'image', message: { url: 'https://example.com/image.jpg' })

# creating an MMS audio message with optional properties
message = Vonage::Messaging::Message.mms(type: 'audio', message: { url: 'https://example.com/audio.mp3' }, opts: {client_ref: "abc123"})

Once the message data is created, you can then send the message.

response = client.messaging.send(to: "447700900000", from: "447700900001", **message)

Verify API v2

The Vonage Verify API v2 allows you to manage 2FA verification workflows over a number of different channels such as SMS, WhatsApp, WhatsApp Interactive, Voice, Email, and Silent Authentication, either individually or in combination with each other. See the Vonage Developer Documentation for a complete API reference listing all the channels, verification options, and callback types.

The Ruby SDK provides two methods for interacting with the Verify v2 API:

  • Verify2#start_verification: starts a new verification request. Here you can specify options for the request and the workflow to be used.
  • Verify2#check_code: for channels where the end-user is sent a one-time code, this method is used to verify the code against the request_id of the verification request created by the start_verification method.

Creating a Verify2 Object

verify = client.verify2

Making a verification request

For simple requests, you may prefer to manually set the value for workflow (an array of one or more hashes containing the settings for a particular channel) and any optional params.

Example with the required :brand and :workflow arguments:

verification_request = verify.start_verification(
  brand: 'Acme',
  workflow: [{channel: 'sms', to: '447000000000'}]
)

Example with the required :brand and :workflow arguments, and an optional code_length:

verification_request = verify.start_verification(
  brand: 'Acme',
  workflow: [{channel: 'sms', to: '447000000000'}],
  code_length: 6
)

For more complex requests (e.g. with mutliple workflow channels or addtional options), or to take advantage of built-in input validation, you can use the StartVerificationOptions object and the Workflow and various channel objects or the WorkflowBuilder:

Create options using StartVerificationOptions object

opts = verify.start_verification_options(
  locale: 'fr-fr',
  code_length: 6,
  client_ref: 'abc-123'
).to_h

verification_request = verify.start_verification(
  brand: 'Acme',
  workflow: [{channel: 'email', to: 'alice.example.com'}],
  **opts
)

Create workflow using Workflow and Channel objects

# Instantiate a Workflow object
workflow = verify.workflow

# Add channels to the workflow
workflow << workflow.sms(to: '447000000000')
workflow << workflow.email(to: 'alice.example.com')

# Channel data is encpsulated in channel objects stored in the Workflow list array
workflow.list
# => [ #<Vonage::Verify2::Channels::SMS:0x0000561474a74778 @channel="sms", @to="447000000000">,
  #<Vonage::Verify2::Channels::Email:0x0000561474c51a28 @channel="email", @to="alice.example.com">]

# To use the list as the value for `:workflow` in a `start_verification` request call,
# the objects must be hashified
workflow_list = workflow.hashified_list
# => [{:channel=>"sms", :to=>"447000000000"}, {:channel=>"email", :to=>"alice.example.com"}]

verification_request = verify.start_verification(brand: 'Acme', workflow: workflow_list)

Create a workflow using the WorkflowBuilder

workflow = verify.workflow_builder.build do |builder|
  builder.add_voice(to: '447000000001')
  builder.add_whatsapp(to: '447000000000')
end

workflow_list = workflow.hashified_list
# => [{:channel=>"voice", :to=>"447000000001"}, {:channel=>"whatsapp", :to=>"447000000000"}]

verification_request = verify.start_verification(brand: 'Acme', workflow: workflow_list)

Cancelling a request

You can cancel in in-progress verification request

# Get the `request_id` from the Vonage#Response object returned by the `start_verification` method call
request_id = verification_request.request_id

verify.cancel_verification_request(request_id: request_id)

Checking a code

# Get the `request_id` from the Vonage#Response object returned by the `start_verification` method call
request_id = verification_request.request_id

# Get the one-time code via user input
# e.g. from params in a route handler or controller action for a form input
code = params[:code]

begin
  code_check = verify.check_code(request_id: request_id, code: code)
rescue => error
  # an invalid code will raise an exception of type Vonage::ClientError
end

if code_check.http_response.code == '200'
  # code is valid
end

Documentation

Vonage Ruby documentation: https://www.rubydoc.info/github/Vonage/vonage-ruby-sdk

Vonage Ruby code examples: https://github.com/Vonage/vonage-ruby-code-snippets

Vonage APIs API reference: https://developer.nexmo.com/api

Frequently Asked Questions

Supported APIs

The following is a list of Vonage APIs and whether the Ruby SDK provides support for them:

API API Release Status Supported?
Account API General Availability βœ…
Alerts API General Availability βœ…
Application API General Availability βœ…
Audit API Beta ❌
Conversation API Beta ❌
Dispatch API Beta ❌
External Accounts API Beta ❌
Media API Beta ❌
Messages API General Availability βœ…
Number Insight API General Availability βœ…
Number Management API General Availability βœ…
Pricing API General Availability βœ…
Redact API Developer Preview βœ…
Reports API Beta ❌
SMS API General Availability βœ…
Verify API General Availability βœ…
Verify API v2 General Availability βœ…
Voice API General Availability βœ…

License

This library is released under the Apache 2.0 License

More Repositories

1

vonage-php-sdk-core

Vonage REST API client for PHP. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
PHP
917
star
2

vonage-node-sdk

Vonage API client for Node.js. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
TypeScript
382
star
3

vonage-python-sdk

Vonage Server SDK for Python. API support for Voice, SMS, WhatsApp, Verify (2FA), Video Meetings and more.
Python
193
star
4

Grafana_Status_panel

A panel plugin for Grafana to monitor multiple parameters at once
TypeScript
179
star
5

vonage-php-sdk

Wrapper package to bring in the vonage/client-core code and an HTTP client
109
star
6

vonage-dotnet-sdk

Vonage REST API client for .NET, written in C#. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
C#
106
star
7

vonage-java-sdk

Vonage Server SDK for Java. API support for SMS, Messages, Voice, Text-to-Speech, Numbers, Verify (2FA), Video and more.
Java
97
star
8

vonage-node-code-snippets

NodeJS code examples for using Vonage
JavaScript
70
star
9

vivid-3

Vonage's web UI 🎨 toolbelt
TypeScript
54
star
10

vonage-go-sdk

A lightweight library to help Go users everywhere integrate with the Vonage APIs. Issues and PRs all really welcome!!
Go
51
star
11

vivid

Vonage's web UI 🎨 toolbelt
JavaScript
42
star
12

rxzu

RxZu Diagrams
TypeScript
41
star
13

vonage-laravel

Laravel Service Provider for integrating Vonage APIs
PHP
34
star
14

vonage-python-code-snippets

Python code examples for using Vonage communications APIs
Python
32
star
15

vonage-php-code-snippets

PHP code examples for using Vonage APIs
PHP
29
star
16

micro-frontends

TypeScript
27
star
17

vonage-curl-code-snippets

Curl examples for working with Vonage APIs
Shell
26
star
18

gosrvlib

Go library to provide base service components
Go
20
star
19

vonage-cli

Vonage CLI tool.
TypeScript
18
star
20

vonage-java-code-snippets

Java code examples for using Vonage
Java
11
star
21

vonage-dotnet-code-snippets

.NET code samples for using Nexmo
C#
11
star
22

kafka-glue

Kafka Glue is a collection of libraries for kafka consumers and producers integrated with AWS Glue schema registry with RXJS to expose streaming of the kafka.
TypeScript
8
star
23

vonage-ruby-code-snippets

Ruby code examples for working with Vonage
Ruby
8
star
24

vbc-developer

Vonage Developer Portal documentation
HTML
6
star
25

vivid-react

Typescript friendly Reactjs :atom: wrappers/bindings for Vonage's web UI 🎨 toolbelt
JavaScript
5
star
26

acl-express

A stateless Access Control List middleware for Express
JavaScript
5
star
27

vonage-go-code-snippets

Repo of "howto" examples for using Vonage APIs with Go.
Go
4
star
28

cloud-runtime-cli

Vonage cloud runtime - CLI
Go
4
star
29

vonage-firebase-extensions

Firebase Extensions created by Vonage
JavaScript
3
star
30

sippy

A mirror of python-sippy b2bua library (and rtp_cluster): http://b2bua.org/
Python
3
star
31

aws-systems-manager-toolkit

aws-systems-manager-toolkit is a Python library that provides wrapper tools around AWS Systems Manager functionality.
Python
3
star
32

verify-silent-auth-sdk-android

This SDK enables making a HTTP request over cellular even when on WiFi.
Kotlin
3
star
33

vonage-media-transformers-samples

Vonage Media Transformers Examples
TypeScript
3
star
34

numkey

Numerical Encoding for Short Codes or E.164 LVN
C
3
star
35

video-express-plus-sample

This Repository demonstrates how to use Vonage Video Express Plus
HTML
2
star
36

vonage-python-jwt

A JWT Generator for Python. Creates JWTs for use with Vonage APIs.
Python
2
star
37

vivid-bindings-vue

Fully automated typescript bindings generated for Vivid web components to be used in VueJS
TypeScript
2
star
38

vivid-react-wrapper

A thin wrapper around an arbitrary custom HTML element to mitigate the gaps in :atom: React versions until 19.x
TypeScript
2
star
39

vonage-client-sdk-ios

Swift
1
star
40

vscode

A Visual Studio Code extension for accessing the Vonage communication APIs.
TypeScript
1
star
41

api-specification

JavaScript
1
star
42

cli-plugin-scaffold

TypeScript
1
star
43

vivid-design-tokens-properties

JavaScript
1
star
44

exprees-socket-server

JavaScript
1
star
45

vonage-jwt-jdk

Library to assist with generating JWT tokens in JVM-based languages for use with the Vonage API.
Kotlin
1
star
46

vonage-kotlin-sdk

Vonage Server SDK for Kotlin. API support for SMS, RCS, Messages, Voice, Text-to-Speech, Numbers, Verify (2FA), Video and more.
Kotlin
1
star