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:

  • Minimum 8 characters
  • Maximum 25 characters
  • Minimum 1 lower case character
  • Minimum 1 upper case character
  • Minimum 1 digit
Key Description
VONAGE_API_KEY Your Vonage API key (see it on your dashboard).
VONAGE_API_SECRET Your Vonage API secret (also available on your dashboard).
NEW_SECRET The new API secret for the API key.

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 "$VONAGE_API_KEY:$VONAGE_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

npm install @vonage/server-sdk

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

Copy to Clipboard
const Vonage = require('@vonage/server-sdk')

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET
})

View full source

Write the code

Add the following to create-secret.js:

Copy to Clipboard
vonage.account.createSecret(VONAGE_API_KEY, NEW_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

Add the following to `build.gradle`:

compile 'com.vonage:client:5.5.0'

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

Copy to Clipboard
VonageClient client = VonageClient.builder()
        .apiKey(VONAGE_API_KEY)
        .apiSecret(VONAGE_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(VONAGE_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:

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

Run the following gradle command to execute your application, replacing com.vonage.quickstart.account with the package containing CreateSecret:

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

Prerequisites

Install-Package Vonage

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

Copy to Clipboard
using Vonage.Accounts;
using Vonage;
using Vonage.Request;

View full source

Add the following to CreateSecret.cs:

Copy to Clipboard
var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(credentials);

View full source

Write the code

Add the following to CreateSecret.cs:

Copy to Clipboard
var request = new CreateSecretRequest() { Secret = NEW_SECRET };

var response = client.AccountClient.CreateApiSecret(request, API_KEY);

View full source

Prerequisites

composer require vonage/client

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

Copy to Clipboard
$basic  = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client($basic);

View full source

Write the code

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

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

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

View full source

Run your code

Save this file to your machine and run it:

php create-a-secret.php

Prerequisites

pip install vonage

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

Copy to Clipboard
client = vonage.Client(
    key=os.getenv("VONAGE_API_KEY"), secret=os.getenv("VONAGE_API_SECRET")
)

View full source

Write the code

Add the following to create-secret.py:

Copy to Clipboard
try:
    response = client.create_secret(api_key, new_api_secret)
    if "id" in response:
        print(
            "Secret was created\nId: {}\nCreated at: {}\nLinks: {}".format(
                response["id"],
                response["created_at"],
                response["_links"]["self"]["href"],
            )
        )
except:
    print(
        "Error: Secret does not meet complexity requirements. Please check the link below for more details:\n",
        "https://developer.nexmo.com/api-errors/account/secret-management#validation",
    )

View full source

Run your code

Save this file to your machine and run it:

python3 create-secret.py

Prerequisites

gem install vonage

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

Copy to Clipboard
client = Vonage::Client.new(
  api_key: VONAGE_API_KEY,
  api_secret: VONAGE_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