Create an Application

In this code snippet you will see how to create an Application.

Example

You will need to ensure that the following replaceable values are set in the example code using any convenient method:

Key Description
NEXMO_API_KEY Your Nexmo API key.
NEXMO_API_SECRET Your Nexmo API secret.

Write the code

Add the following to create-application.sh:

Copy to Clipboard
curl -X "POST" "https://api.nexmo.com/v2/applications" \
     -H 'Content-Type: application/json' \
     -u "$NEXMO_API_KEY:$NEXMO_API_SECRET" \
     -d $'{
  "name": "Code Snippets V2 Application",
  "capabilities": {
    "messages": {
      "webhooks": {
        "inbound_url": {
          "address": "https://example.com/webhooks/inbound",
          "http_method": "POST"
        },
        "status_url": {
          "address": "https://example.com/webhooks/status",
          "http_method": "POST"
        }
      }
    }
  }
}'

View full source

Run your code

Save this file to your machine and run it:

./create-application.sh

Prerequisites

Install dependencies

npm install nexmo
Initialize your dependencies

Create a file named create-application-v2.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo')
   
var nexmo = new Nexmo({
    apiKey: NEXMO_API_KEY,
    apiSecret: NEXMO_API_SECRET
}, {
    debug: true
});

View full source

Write the code

Add the following to create-application-v2.js:

Copy to Clipboard
nexmo.applications.create({
    name: 'My nexmo-node Example V2 App',
    capabilities: {
        voice: {
            webhooks: {
                answer_url: {
                    address: "https://example.com/webhooks/answer",
                    http_method: "GET"
                },
                event_url: {
                    address: "https://example.com/webhooks/event",
                    http_method: "POST"
                }
            }
        },
        messages: {
            webhooks: {
                inbound_url: {
                    address: "https://example.com/webhooks/inbound",
                    http_method: "POST"
                },
                status_url: {
                    address: "https://example.com/webhooks/status",
                    http_method: "POST"
                }
            }
        },
        rtc: {
            webhooks: {
                event_url: {
                    address: "https://example.com/webhooks/rtcevent",
                    http_method: "POST"
                }
            }
        }
    }
}, (error, result) => {
    if(error) {
        console.error(error);
    }
    else {
        console.log(result);
    }
});

View full source

Run your code

Save this file to your machine and run it:

node create-application-v2.js

Prerequisites

Install dependencies

Add the following to build.gradle:

compile 'com.nexmo:client:5.1.0'
Initialize your dependencies

Create a class named CreateApplication and add the following code to the main method:

Copy to Clipboard
NexmoClient client = NexmoClient.builder()
        .apiKey(NEXMO_API_KEY)
        .apiSecret(NEXMO_API_SECRET)
        .build();

View full source

Write the code

Add the following to the main method of the CreateApplication class:

Copy to Clipboard
ApplicationClient applicationClient = client.getApplicationClient();

Capability messages = Messages.builder()
        .addWebhook(Webhook.Type.INBOUND,
                new Webhook("https://example.com/webhooks/inbound", HttpMethod.POST))
        .addWebhook(Webhook.Type.STATUS,
                new Webhook("https://example.com/webhooks/status", HttpMethod.POST))
        .build();

Application application = applicationClient.createApplication(
        Application.builder()
                .name(NAME)
                .addCapability(messages)
                .build()
);

System.out.println("Application Created:");
System.out.println(application.toJson());

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.nexmo.quickstart.application with the package containing CreateApplication:

gradle run -Pmain=com.nexmo.quickstart.application.CreateApplication

Prerequisites

Install dependencies

Install-Package Nexmo.Csharp.Client
Initialize your dependencies

Create a file named ApplicationSender.cs and add the following code:

Copy to Clipboard
const string API_KEY = "NEXMO_API_KEY";
const string API_SECRET = "NEXMO_API_SECRET";

var client = new Client(creds: new Nexmo.Api.Request.Credentials(
    nexmoApiKey: API_KEY, nexmoApiSecret: API_SECRET));

View full source

Write the code

Add the following to ApplicationSender.cs:

Copy to Clipboard
var request = new AppRequest()
{                
    Name = name,
    Capabilities = new Capabilities()
    {
        Messages = new MessagesWebhook(
            new Webhook() { Address = "https://example.com/webhooks/inbound", HttpMethod = "POST" },
            new Webhook() { Address = "https://example.com/webhooks/status", HttpMethod = "POST" }),
        Rtc = new RtcWebhook(
            new Webhook() { Address = "https://example.com/webhooks/event", HttpMethod = "POST" }),
        Voice = new VoiceWebhook(
            new Webhook() { Address = "https://example.com/webhooks/answer", HttpMethod = "GET" },
            new Webhook() { Address = "https://example.com/webhooks/event", HttpMethod = "POST" }),
        Vbc = new VbcWebhook()
    }
};

var response = client.ApplicationV2.Create(request: request);

View full source

Prerequisites

Install dependencies

composer require nexmo/client
Initialize your dependencies

Create a file named create-application.php and add the following code:

Copy to Clipboard
$basic  = new \Nexmo\Client\Credentials\Basic(NEXMO_API_KEY, NEXMO_API_SECRET);
$client = new \Nexmo\Client(new \Nexmo\Client\Credentials\Container($basic));

View full source

Write the code

Add the following to create-application.php:

Copy to Clipboard
try {
    $application = $client->applications()->create(
        [
            'name' => 'Sample PHP V2 Application',
            'capabilities' => [
                'voice' => [
                    'webhooks' => [
                        'answer_url' => [
                            'address' => "https://example.com/webhooks/answer",
                            'http_method' => "GET"
                        ],
                        'event_url' => [
                            'address' => "https://example.com/webhooks/event",
                            'http_method' => "POST"
                        ]
                    ]
                ],
                'messages' => [
                    'webhooks' => [
                        'inbound_url' => [
                            'address' => "https://example.com/webhooks/inbound",
                            'http_method' => "POST"
                        ],
                        'status_url' => [
                            'address' => "https://example.com/webhooks/status",
                            'http_method' => "POST"
                        ]
                    ]
                ],
                'rtc' => [
                    'webhooks' => [
                        'event_url' => [
                            'address' => "https://example.com/webhooks/rtcevent",
                            'http_method' => "POST"
                        ]
                    ]
                ]
            ]
        ]
    );

    echo $application->getId() . PHP_EOL;
    echo $application->getName() . PHP_EOL;
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage() . PHP_EOL;
}

View full source

Run your code

Save this file to your machine and run it:

php create-application.php

Prerequisites

Install dependencies

pip install nexmo
Initialize your dependencies

Create a file named create-application.py and add the following code:

Copy to Clipboard
client = nexmo.Client(
    key=NEXMO_API_KEY,
    secret=NEXMO_API_SECRET
)

View full source

Write the code

Add the following to create-application.py:

Copy to Clipboard
response = client.application_v2.create_application({
    "name": "Code Example App",
    "capabilities": {
        "messages": {
            "webhooks": {
                "inbound_url": {
                    "address": "https://example.com/webhooks/inbound",
                    "http_method": "POST"
                },
                "status_url": {
                    "address": "https://example.com/webhooks/status",
                    "http_method": "POST"
                }
            }
        }
    }
})

pprint(response)

View full source

Run your code

Save this file to your machine and run it:

python3 create-application.py

Prerequisites

Install dependencies

gem install nexmo
Initialize your dependencies

Create a file named create-application.rb and add the following code:

Copy to Clipboard
client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_API_SECRET
)

View full source

Write the code

Add the following to create-application.rb:

Copy to Clipboard
begin
  response = client.applications_v2.create(
    name: 'Code Example App',
    capabilities: {
      'messages': {
        'webhooks': {
          'inbound_url': {
            'address': 'https://example.com/webhooks/inbound',
            'http_method': 'POST'
          },
          'status_url': {
            'address': 'https://example.com/webhooks/status',
            'http_method': 'POST'
          }
        }
      }
    }
  )
  puts "Application #{response.id} Created Successfully" if response.id
rescue StandardError => e
  puts e.message
end

View full source

Run your code

Save this file to your machine and run it:

ruby create-application.rb