B1officesEssay17 December 20254-minute read

Wrapping an API so nobody has to read its docs twice

What I learned turning the MetaTrader 5 Web API into a Laravel package other people depend on.

The MetaTrader 5 Web API is how a broker's back office talks to its trading servers: opening accounts, moving money in and out, reading positions and deals. The first time you use it, you read the documentation very carefully. The point of wrapping it in a package is that nobody after you has to.

That package is MetaFive, now meta-trader-client on Packagist. It runs in broker back offices serving 10,000+ users, and most of what I learned building it comes down to one question: what should a wrapper hide, and what should it leave alone?

What the raw API is like

It's called a web API, but it isn't HTTP. It's a raw socket. Commands are lines of text shaped like COMMAND|PARAM=value, encoded as UTF-16, with their own escaping rules for the characters that would break them. Every packet starts with a small header of its own, and a long answer can arrive spread across several packets.

Logging in is a handshake, not a header. The server sends a random challenge, the client answers with a hash built from the password and sends a challenge back, and the server has to prove it knows the password too. Only then does the encrypted part begin.

Every call returns a number. Success is one number among more than a hundred, and the data you asked for doesn't come back as a return value at all. It's written into variables you passed in by reference.

None of that is wrong. It's a protocol designed for speed and for a lot of languages at once. But every Laravel app that talks to it has to learn all of it again, and get it right.

Hide the plumbing

The first rule I settled on: hide the transport completely. Nobody building a back office should know the commands are UTF-16 or that packets have headers. A Laravel developer should ask for an account and get an account.

So the package turns out-parameters into return values, wraps the socket and the handshake, and reads its settings from the environment like any other Laravel package. A service provider and a facade make it feel like part of the framework instead of a visitor.

Keep the vocabulary

The second rule: don't rename the domain. Brokers think in groups, deals, positions and balance operations, and so does the API. A wrapper that invents friendlier words just gives people two vocabularies to translate between. The method names got readable; the concepts stayed exactly where MetaTrader put them.

Errors are part of the API

More than a hundred integer codes is a lot to ask anyone to memorise. The package sorts them into ranges, each with its own exception class: authentication, configuration, user management, trading and so on. You catch the kind of problem you can do something about.

But the original code stays on every exception. When a trade is refused at the wrong moment, the first thing anyone asks for is MetaTrader's own code, and a wrapper that swallows it makes support harder, not easier.

One login per request

Because logging in is a full handshake, doing it for every call would be slow and pointless. The client lives in Laravel's container as one shared instance and connects the first time it's needed. A request that makes twenty calls logs in once.

Data that comes in two shapes

The API has opinions about numbers. Trade volumes come as integers in two different scales depending on the format, so the package has helpers for both, and a server's time can come back as text or as a number. Some answers simply leave fields out. Every one of those turned into a small fix: a helper, a type check, a parser that doesn't fall over when a field is missing. Each fix is a page of the docs nobody else has to read.

What it doesn't hide yet

The honest gap is long-running processes. One connection per request is fine for web requests. A queue worker that runs for hours can outlive its socket, and the package doesn't reconnect on its own yet. Its own Artisan commands already reconnect and retry when the pipe breaks, which is a good sign of where the next version should go.

Break it once, on purpose

The first version kept the shape of the vendor's code, because that was the fastest way to something that worked. Version two broke that on purpose: readable method names, return values, typed exceptions and lazy connections. That's what major versions are for. A wrapper that never breaks its first design ends up wrapping its own mistakes.

What I'd keep

  • Hide the transport completely, and keep the domain's own words.
  • Turn error codes into exceptions people can catch, and keep the raw code.
  • Pay for expensive handshakes once.
  • Every awkward data format is a helper you write so nobody else has to.

A good wrapper is the documentation you wished you'd had, written as code.

Ring the bell

Got a question one of these didn't answer? Ask me directly.

Get in touch