How do I connect to an MT4 or MT5 server from C#, Python or TypeScript?

Install the official WebAPI SDK for your language, build a client with the id and secret of an API client created in Toolbox, and call the server. Examples for C#, Python, TypeScript and PowerShell, plus the raw HTTP route.

Install the CPlugin WebAPI SDK for your language, create a client with the client id and secret of an API client from Toolbox, and call a method with the id of your registered trade platform. The SDK obtains, caches and refreshes the OAuth 2.0 token for you. The same steps work for MT4 and MT5; the environment preset staging points the SDK at the free Sandbox and prod at production.

Before the first line of code

Do this once per project:

  1. Sign in at toolbox.cplugin.com, or at pre.toolbox.cplugin.com for the free Sandbox.
  2. Click Add platform, pick MT4 or MT5 and enter the address, login and password of a manager account on your server.
  3. Open WebAPI → API Clients and create a client. The client secret is shown only once, so store it in a secret manager.

The SDKs are 0.x releases, so pin an exact version.

TypeScript

bun add @mywebapi.com/sdk
import { CPluginWebApiClient } from '@mywebapi.com/sdk'

const client = new CPluginWebApiClient({
  env: 'prod', // or 'staging' for the Sandbox
  clientId: process.env.CPLUGIN_WEBAPI_CLIENT_ID!,
  clientSecret: process.env.CPLUGIN_WEBAPI_CLIENT_SECRET!,
})

const tp = 'your-tradeplatform-uuid' // from Toolbox, on your platform's settings page
const time = await client.mt4.getServerTime(tp)
console.log('MT4 server time:', time)

Package: @mywebapi.com/sdk on npm, source on GitHub.

C#

dotnet add package MyWebApi.Sdk
using CPlugin.SaaSWebApi.Client;

using var client = new CPluginWebApiClient(CPluginEnvironment.Prod, clientId, clientSecret);

var platforms = await client.ListTradePlatformsAsync();
var tp = Guid.Parse(platforms[0]!["id"]!.GetValue<string>());

var mt4 = client.MT4(tp);
var time = await mt4.ServerTimeAsync();
Console.WriteLine($"MT4 server time: {time}");

Package: MyWebApi.Sdk on NuGet, source on GitHub.

Python

Python 3.10 or later; the import name is cplugin_webapi_sdk.

pip install mywebapi-sdk
import os
from cplugin_webapi_sdk import CPluginWebApiClient

with CPluginWebApiClient(
    env="prod",  # or "staging" for the Sandbox
    client_id=os.environ["CPLUGIN_WEBAPI_CLIENT_ID"],
    client_secret=os.environ["CPLUGIN_WEBAPI_CLIENT_SECRET"],
) as client:
    tp = "your-tradeplatform-uuid"  # from Toolbox, on your platform's settings page
    time = client.mt4.get_server_time(tp)
    print("MT4 server time:", time)

Package: mywebapi-sdk on PyPI, source on GitHub.

PowerShell

Install-Module MyWebApi -Scope CurrentUser

$secret = ConvertTo-SecureString $env:CPLUGIN_WEBAPI_CLIENT_SECRET -AsPlainText -Force
$session = Connect-MyWebApi -Environment Production -ClientId $env:CPLUGIN_WEBAPI_CLIENT_ID -ClientSecret $secret

$tp = (Get-MyWebApiTradePlatform -Connection $session)[0].id
Get-MT4ServerTime -Connection $session -TradePlatform $tp

Module: MyWebApi in the PowerShell Gallery.

Without an SDK

Any HTTP client works. Exchange the client id and secret for a bearer token (OAuth 2.0, Client Credentials flow, described in Authorization), then send it in the Authorization header:

curl https://cloud.mywebapi.com/api/v2/MT4/{tradePlatform}/ServerTime \
  -H "Authorization: Bearer $ACCESS_TOKEN"

Errors, timeouts and retries

Errors come back as a typed ApiError in TypeScript and .NET, and as the error field in every v2 response, carrying a stable code and an activityId you can give to support. Each call has a default deadline that you can change per call, and no SDK repeats a trade or a change by itself. Read Timeouts and retries before you repeat a trade, because a timeout does not mean the operation failed.

Next