mygrainexchange / mgx-php
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.
1.0.0
2026-06-19 19:45 UTC
Requires
- php: ^8.1
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.3
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- phpunit/phpunit: ^8.0 || ^9.0
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 optionallyrefreshToken) toMgxClient.
Features
- Resource namespaces —
inventory(),market(),bids(),trades(),teams(),cashBids(),webhooks(). - Auto-pagination — every
list()returns aPaginatoryou canforeachover; it followsnextuntil the result set is exhausted (->toArray()collects eagerly). - Idempotency —
inventory()->placeBid()andcashBids()->create()send anIdempotency-Keyautomatically (override with the$idempotencyKeyargument). - Typed errors — non-2xx responses throw
MgxApiErrorwithstatus(),code(),getMessage(), andfieldErrors().
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