/nexmo-java

Nexmo REST API Client Library for Java. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.

Primary LanguageJavaMIT LicenseMIT

Nexmo Client Library for Java

Build Status codecov Codacy Badge

You can use this Java client library to add Nexmo's API to your application. To use this, you'll need a Nexmo account. Sign up for free at nexmo.com.

Installation

To use the client library you'll need to have created a Nexmo account.

Gradle

To install the Java client library using Gradle, add the following to build.gradle:

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.nexmo:client:2.0.2'
}

Maven

Add the following to the correct place in your project's POM file:

<dependency>
      <groupId>com.nexmo</groupId>
      <artifactId>client</artifactId>
      <version>2.0.2</version>
</dependency>

Build It Yourself

Alternatively you can clone the repo and build the JAR file yourself:

git clone git@github.com:nexmo/nexmo-java.git
gradle build

Download everything in a ZIP file

Note: We strongly recommend that you use a tool that supports dependency management, such as Gradle, Maven, or Ivy

We provide a ZIP file for each release, containing the client library JAR, along with all the dependencies. Download the file, unzip it, and add the JAR files to your project's classpath.

Usage

Check the Javadoc for full reference documentation. There are also many useful code samples in the nexmo-community/nexmo-java-quickstart repository.

Examples

The following code initiates an outbound call which then reads the user a message:

import java.nio.file.Paths;

import com.nexmo.client.NexmoClient;
import com.nexmo.client.auth.JWTAuthMethod;
import com.nexmo.client.voice.Call;
import com.nexmo.client.voice.CallEvent;

JWTAuthMethod auth = new JWTAuthMethod(application_id, Paths.get("application_key.pem"));
NexmoClient client = new NexmoClient(auth);
Call call = new Call(to, from,
                     "https://nexmo-community.github.io/ncco-examples/first_call_talk.json");
CallEvent event = client.getVoiceClient().createCall(call);

After the call is answered, you can get more information about it, including the amount it cost with:

CallInfo info = client.getVoiceClient().getCallDetails(event.getUuid());
System.out.println("This cost: " + info.getPrice() + " EUR");

You can modify an existing call in progress, for example by hanging up on the current call:

ModifyCallResponse modifyResponse = client.getVoiceClient().modifyCall(event.getUuid(), "hangup");
System.out.println(modifyResponse.getMessage());

While a call is in progress, you can send Dual-tone multi-frequency(DTMF) tones like so:

DtmfResponse dtmfResponse = client.getVoiceClient().sendDtmf(event.getUuid(), "332393");
System.out.println("Success! " + dtmfResponse.getMessage());

To stream an audio file to an active call, simply use the following method:

StreamResponse startStreamResponse = client.getVoiceClient().startStream(event.getUuid(), "https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3");
System.out.println("Success! " + startStreamResponse.getMessage());

If you'd like to stop streaming an audio file to an active call, you can do so with:

StreamResponse stopStreamResponse = client.getVoiceClient().stopStream(event.getUuid());
System.out.println("Alright. " + stopStreamResponse.getMessage());

To send a synthesized speech message to an active call, just use the following method:

TalkResponse startTalkResponse = client.getVoiceClient().startTalk(event.getUuid(), "Hello World");
System.out.println("Success! " + startTalkResponse.getMessage());

If you'd like to stop sending a synthesized speech message to an active call, you can do so with:

TalkResponse stopTalkResponse = client.getVoiceClient().stopTalk(event.getUuid());
System.out.println("Alright. " + stopTalkResponse.getMessage());

API Coverage

  • Account
    • Balance
    • Pricing
    • Settings
    • Top Up
    • Numbers
      • Search
      • Buy
      • Cancel
      • Update
  • Number Insight
    • Basic
    • Standard
    • Advanced
    • Webhook Notification
  • Verify
    • Verify
    • Check
    • Search
    • Control
  • Messaging
    • Send
    • Delivery Receipt (Callback can only be set in the Dashboard)
    • Inbound Messages
    • Search
      • Message
      • Messages
      • Rejections
    • US Short Codes
      • Two-Factor Authentication
      • Event Based Alerts
        • Sending Alerts
        • Campaign Subscription Management
  • Voice
    • Create call
    • List calls
    • Get call info
    • Modify existing call
    • Stream audio to an existing call
    • Stop streaming audio to an existing call
    • Send speech to an existing call
    • Stop speech in an existing call
    • Send DTMF to an existing call
    • eventUrl webhook support
    • answerUrl webhook support

License

This library is released under the MIT License

Contribute!

We ❤️ contributions to this library!

It is a good idea to talk to us first if you plan to add any new functionality. Otherwise, bug reports, bug fixes and feedback on the library is always appreciated.