Join us in San Francisco on the 29/30th of October for two days of developer workshops and technical talks

Create a secret

To create a new API secret, you must send a POST request to the secret management API.

New API secrets must meet the following rules:

Write the code

Add the following to create-secret.sh:

Copy to Clipboard
curl -X POST "https://api.nexmo.com/accounts/$ACCOUNT_ID/secrets" \
     -H 'Content-Type: application/json' \
     -u "$NEXMO_API_KEY:$NEXMO_API_SECRET" \
     -d '{"secret":"Th1s-I5-my_n3w-s3cr3t"}'

View full source

Run your code

Save this file to your machine and run it:

bash create-secret.sh

Prerequisites

Install dependencies

npm install nexmo
Initialize your dependencies

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

Copy to Clipboard
const Nexmo = require('nexmo')

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET
})

View full source

Write the code

Add the following to create-secret.js:

Copy to Clipboard
const secret = 'awes0meNewSekret!!;';

nexmo.account.createSecret(NEXMO_API_KEY, secret, (err, result) => {
    if (err) {
        console.log("Error: " + err.statusCode);
        console.log(err.body);
    } else {
        console.log(result.id, result.created_at);
    }
});

View full source

Run your code

Save this file to your machine and run it:

node create-secret.js

Prerequisites

Install dependencies

Add the following to build.gradle:

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

Create a class named CreateSecret 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 CreateSecret class:

Copy to Clipboard
AccountClient accountClient = client.getAccountClient();

SecretResponse response = accountClient.createSecret(NEXMO_API_KEY, NEW_SECRET);
System.out.println(response.getId() + " created at " + response.getCreated());

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.account with the package containing CreateSecret:

gradle run -Pmain=com.nexmo.quickstart.account.CreateSecret

Prerequisites

Install dependencies

Install-Package Nexmo.Csharp.Client
Initialize your dependencies

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

Copy to Clipboard
var client = new Client(creds: new Nexmo.Api.Request.Credentials
{
    ApiKey = "NEXMO_API_KEY",
    ApiSecret = "NEXMO_API_SECRET"
});

View full source

Write the code

Add the following to AccountController.cs:

Copy to Clipboard
var result = Client.ApiSecret.Create(NEXMO_API_KEY, newSecret);

if (result != null)
{
    ViewBag.creationResult = $"Secret created! Your secret Id is  {result.Id}";
}

View full source

Prerequisites

Install dependencies

composer require nexmo/client
Initialize your dependencies

Create a file named create-a-secret.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($basic);

View full source

Write the code

Add the following to create-a-secret.php:

Copy to Clipboard
$secret = 'awes0meNewSekret!!;';

$client->account()->createSecret(NEXMO_API_KEY, $secret);

View full source

Run your code

Save this file to your machine and run it:

php create-a-secret.php

Prerequisites

Install dependencies

gem install nexmo
Initialize your dependencies

Create a file named create-a-secret.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-a-secret.rb:

Copy to Clipboard
begin
  response = client.secrets.create(secret: 'Th1s-I5-my_n3w-s3cr3t')
  puts 'Secret Created Successfully' if response.created_at
rescue StandardError => e
  puts e.message
end

View full source

Run your code

Save this file to your machine and run it:

ruby create-a-secret.rb