Official PHP client for the MGX Enterprise API. Browse anonymized inventory, place bids, read your team's trades and bids, manage cash bids, subscribe to webhooks, and read market prices. Authenticate with OAuth2 (Login with MGX). Seller identity is never exposed until a trade is paid on both sides.

Maintainers

Package info

github.com/mygrainexchange/mgx-php

Homepage

pkg:composer/mygrainexchange/mgx-php

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-06-19 19:45 UTC

This package is auto-updated.

Last update: 2026-06-19 19:46:45 UTC


README

Official PHP client for the MGX Enterprise API.

composer require mygrainexchange/mgx-php

Requires PHP 8.1+.

Quickstart

use MyGrainExchange\Mgx\Overlay\MgxClient;
use MyGrainExchange\Mgx\Overlay\MgxApiError;

$mgx = new MgxClient(
    clientId: getenv('MGX_CLIENT_ID'),
    clientSecret: getenv('MGX_CLIENT_SECRET'),
    scopes: ['inventory.read', 'market.read'],
    // baseUrl: 'https://dashboard.mgx.test/v1', // for local development
);

// Auto-paginates the { items, limit, offset, next } envelope.
foreach ($mgx->inventory()->list(['commodity' => 'wheat', 'minQuantity' => 50]) as $lot) {
    echo $lot->getId(), ' ', $lot->getQuantityMt(), ' ', $lot->getAskingPrice()?->getAmount(), PHP_EOL;
}

You can also construct with an options array:

$mgx = MgxClient::create([
    'clientId' => getenv('MGX_CLIENT_ID'),
    'clientSecret' => getenv('MGX_CLIENT_SECRET'),
    'scopes' => ['inventory.read', 'market.read'],
]);

Authentication

  • Client credentials (read-only data) — pass clientId + clientSecret + scopes; the SDK acquires, caches, and refreshes the token for you.
  • Login with MGX (user-context: bids, trades, teams, cash bids, webhooks) — complete the authorization-code + PKCE flow, then pass the resulting accessToken (and optionally refreshToken) to MgxClient.

Features

  • Resource namespacesinventory(), market(), bids(), trades(), teams(), cashBids(), webhooks().
  • Auto-pagination — every list() returns a Paginator you can foreach over; it follows next until the result set is exhausted (->toArray() collects eagerly).
  • Idempotencyinventory()->placeBid() and cashBids()->create() send an Idempotency-Key automatically (override with the $idempotencyKey argument).
  • Typed errors — non-2xx responses throw MgxApiError with status(), code(), getMessage(), and fieldErrors().
try {
    $bid = $mgx->inventory()->placeBid('inv_3Kd9aZ', [
        'quantity_mt' => 50,
        'price' => ['amount' => 312.5],
        'delivery' => ['from' => '2026-08-01', 'to' => '2026-09-30'],
    ]);
} catch (MgxApiError $e) {
    error_log("{$e->status()} {$e->code()} {$e->getMessage()}");
}

Generated code

The lib/ client is generated from the OpenAPI spec; the ergonomic layer lives in overlay/ (namespace MyGrainExchange\Mgx\Overlay). Do not hand-edit lib/ — change the API spec and regenerate. See the mgx-sdks repo.

License

MIT