Skip to main content

Overview

The UserApi provides comprehensive methods for managing MetaTrader 5 user accounts. This guide covers user creation, updates, deposits, withdrawals, password resets, and account deletion.

Creating a New User

Use the userAddPost() method to create a new trading account.
1

Prepare User Data

Create a User model with required account information.
use D4T\MT5Sdk\MT5Manager\UserApi;
use D4T\MT5Sdk\Models\User;
use D4T\MT5Sdk\Configuration;
use GuzzleHttp\Client;

// Configure API
$config = Configuration::getDefaultConfiguration()
    ->setHost('https://your-mt5-server.com/api')
    ->setAccessToken('your_bearer_token');

$userApi = new UserApi(new Client(), $config);

// Prepare user data
$newUser = new User();
$newUser->setName('John Doe');
$newUser->setEmail('john.doe@example.com');
$newUser->setGroup('demo\\demoforex');
$newUser->setPassword('SecurePass123!');
$newUser->setPasswordInvestor('InvestorPass123!');
$newUser->setLeverage(100);
2

Create the User

Call the userAddPost() method to create the account.
try {
    $result = $userApi->userAddPost($newUser);
    
    echo "User created successfully!\n";
    echo "Login ID: " . $result->getLogin() . "\n";
    echo "Status: " . $result->getRetcode() . "\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Error creating user: " . $e->getMessage() . "\n";
}

Updating User Information

Update existing user account details using the updateUser() method.
use D4T\MT5Sdk\Models\User;

$userUpdate = new User();
$userUpdate->setLogin(12345);
$userUpdate->setName('John Smith'); // Updated name
$userUpdate->setPassword('NewSecurePass456!');

try {
    $userApi->updateUser($userUpdate);
    echo "User updated successfully!\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Update failed: " . $e->getMessage() . "\n";
}

Retrieving User Information

Fetch user details by login ID using userUserLoginGet().
try {
    $user = $userApi->userUserLoginGet('12345');
    
    echo "User Details:\n";
    echo "Login: " . $user->getLogin() . "\n";
    echo "Name: " . $user->getName() . "\n";
    echo "Email: " . $user->getEmail() . "\n";
    echo "Group: " . $user->getGroup() . "\n";
    echo "Leverage: " . $user->getLeverage() . "\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

Account Balance Operations

Depositing Funds

Add funds to a user account with the userDepositPost() method.
use D4T\MT5Sdk\Models\BalanceType;

$deposit = new BalanceType();
$deposit->setLogin(12345);
$deposit->setAmount(1000.00);
$deposit->setType(3); // Balance operation type
$deposit->setComment('Initial deposit');

try {
    $userApi->userDepositPost($deposit);
    echo "Deposit successful!\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Deposit failed: " . $e->getMessage() . "\n";
}

Password Management

Reset user passwords using the userResetPwdPost() method.
use D4T\MT5Sdk\Models\ResetPwdType;

$passwordReset = new ResetPwdType();
$passwordReset->setLogin(12345);
$passwordReset->setPassword('NewPassword789!');
$passwordReset->setChangeInvestor(0); // 0 = main password, 1 = investor password

try {
    $userApi->userResetPwdPost($passwordReset);
    echo "Password reset successfully!\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Password reset failed: " . $e->getMessage() . "\n";
}
Set change_investor to 1 to reset the investor (read-only) password instead of the main trading password.

Deleting a User

Remove a user account using userUserLoginDelete().
try {
    $userApi->userUserLoginDelete('12345');
    echo "User deleted successfully!\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Deletion failed: " . $e->getMessage() . "\n";
}
User deletion is permanent and cannot be undone. Ensure you have proper authorization and backup procedures before deleting accounts.

Getting Users by Group

Retrieve all users in a specific trading group.
try {
    $groupName = 'demo\\demoforex';
    $cachedLogins = $userApi->usersGroupGet($groupName);
    
    echo "Users in group: " . $groupName . "\n";
    foreach ($cachedLogins->getLogins() as $login) {
        echo "Login: " . $login . "\n";
    }
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

Complete Working Examples

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use D4T\MT5Sdk\MT5Manager\UserApi;
use D4T\MT5Sdk\Models\User;
use D4T\MT5Sdk\Models\BalanceType;
use D4T\MT5Sdk\Configuration;
use GuzzleHttp\Client;

$config = Configuration::getDefaultConfiguration()
    ->setHost('https://your-mt5-server.com/api')
    ->setAccessToken('your_bearer_token');

$userApi = new UserApi(new Client(), $config);

// Step 1: Create new user
$newUser = new User();
$newUser->setName('Jane Trader');
$newUser->setEmail('jane@example.com');
$newUser->setGroup('demo\\demoforex');
$newUser->setPassword('SecurePass123!');
$newUser->setPasswordInvestor('InvestorPass123!');
$newUser->setLeverage(200);

try {
    $result = $userApi->userAddPost($newUser);
    $login = $result->getLogin();
    echo "User created with login: $login\n";
    
    // Step 2: Make initial deposit
    $deposit = new BalanceType();
    $deposit->setLogin($login);
    $deposit->setAmount(10000.00);
    $deposit->setType(3);
    $deposit->setComment('Initial deposit');
    
    $userApi->userDepositPost($deposit);
    echo "Initial deposit of $10,000 completed\n";
    
    // Step 3: Verify user details
    $user = $userApi->userUserLoginGet($login);
    echo "User verified: " . $user->getName() . "\n";
    echo "Email: " . $user->getEmail() . "\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

API Methods Reference

MethodDescriptionReturns
userAddPost($user)Create a new user accountUserReturnType
updateUser($user)Update existing user informationvoid
userUserLoginGet($login)Retrieve user details by loginUser
userDepositPost($balance)Deposit funds to accountvoid
userWithdrawPost($balance)Withdraw funds from accountvoid
userResetPwdPost($reset)Reset user passwordvoid
userUserLoginDelete($login)Delete a user accountvoid
usersGroupGet($group)Get all users in a groupCachedLogins

Best Practices

  1. Password Security: Use strong passwords with uppercase, lowercase, numbers, and special characters
  2. Validation: Validate email addresses and user input before creating accounts
  3. Balance Operations: Always include descriptive comments for deposits and withdrawals for audit trails
  4. Error Handling: Implement comprehensive error handling for all user operations
  5. Group Naming: Use proper group path format with escaped backslashes (e.g., demo\\demoforex)
  6. Leverage Limits: Set appropriate leverage based on user experience and risk management policies
When performing balance operations (deposits/withdrawals), ensure you have proper authorization and compliance with financial regulations. All transactions should be logged for audit purposes.

Next Steps