Create a User
In this code snippet you learn how to create a User.
Example
Ensure the following variables are set to your required values using any convenient method:
| Key | Description |
|---|---|
USER_NAME | The unique name of the User. |
USER_DISPLAY_NAME | The display name of the User. |
Prerequisites
You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
Write the code
Add the following to create-user.sh:
curl -X "POST" "https://api.nexmo.com/v1/users" \
-H 'Authorization: Bearer '$JWT\
-H 'Content-Type: application/json' \
-d $'{
"name": "$USER_NAME",
"display_name": "$USER_DISPLAY_NAME"
}'Run your code
Save this file to your machine and run it:
Prerequisites
You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
npm install @vonage/server-sdkCreate a file named create-user.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH,
});Write the code
Add the following to create-user.js:
vonage.users.createUser({
'name': USER_NAME,
'displayName': USER_DISPLAY_NAME,
})
.then((user) => console.log(user))
.catch((error) => console.error(error));Run your code
Save this file to your machine and run it:
Prerequisites
You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:1.1.2'Create a file named CreateUser and add the following code to the main method:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
}Write the code
Add the following to the main method of the CreateUser file:
val user = client.users.create {
name(USER_NAME)
displayName(USER_DISPLAY_NAME)
imageUrl(IMAGE_URL)
channels(
Pstn(PSTN),
Sms(TO_NUMBER),
Viber(TO_NUMBER),
Whatsapp(TO_NUMBER),
Viber(TO_NUMBER),
Messenger(FB_RECIPIENT_ID),
Vbc(VBC_EXTENSION),
Sip(SIP_SECURE_URI, SIP_USERNAME, SIP_PASSWORD),
Websocket(WEBSOCKET_URI)
)
}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.vonage.quickstart.kt.users with the package containing CreateUser:
Prerequisites
You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:8.15.1'Create a file named CreateUser and add the following code to the main method:
VonageClient client = VonageClient.builder()
.apiKey(VONAGE_API_KEY)
.apiSecret(VONAGE_API_SECRET)
.build();Write the code
Add the following to the main method of the CreateUser file:
User user = client.getUsersClient().createUser(
User.builder()
.name(USER_NAME)
.displayName(USER_DISPLAY_NAME)
.imageUrl("https://example.com/profile.jpg")
.channels(
new Pstn("448001234567"),
new Sms("447700900000"),
new Viber("447700900000"),
new Whatsapp("447700900000"),
new Viber("447700900000"),
new Messenger("12345abcd"),
new Vbc(123),
new Sip("sip:4442138907@sip.example.com;transport=tls", "myUserName", "P@ssw0rd"),
new Websocket("wss://example.com/socket")
)
.build()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.vonage.quickstart.users with the package containing CreateUser:
Prerequisites
You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
pip install vonageWrite the code
Add the following to create-user.py:
from vonage import Auth, Vonage
from vonage_users import Channels, PstnChannel, User
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
)
user_options = User(
name=USER_NAME,
display_name=USER_DISPLAY_NAME,
channels=Channels(pstn=[PstnChannel(number=123456)]),
)
user = client.users.create_user(user_options)
print(user)Run your code
Save this file to your machine and run it:
Try it out
When you run the code you will create a new User.