Skip to main content
This guide walks you through connecting a Trench account to your application, ending with an access token you can trade with. There are three moving parts: a popup that you open, a callback page that you host, and a token exchange that runs on your backend.

Prerequisites

Before starting, make sure you have:
  • An app registered at tren.ch/partner, giving you a client_id and an API key. See Registration.
  • At least one redirect URI registered, matching exactly what you will send.
  • A backend that can hold the client secret, since it must never reach the browser.

Endpoints Used

  • GET https://tren.ch/oauth/authorize for the consent screen
  • POST /oauth/token to exchange the code
Generate a PKCE verifier and a state value, then open the popup. The window needs to be opened synchronously inside the click handler, otherwise the browser will block it.
All six parameters are required. The popup runs on the Trench origin, so the user sees tren.ch in the address bar and can confirm who they are approving. Your page cannot read anything inside it.

2. Relay the Code From Your Callback

The popup lands on your redirect_uri with ?code=…&state=… in the query string. At that point it is same-origin with the opener, so it can post the code back and close itself.
If the user declines, you will receive ?error=access_denied&state=… and no code.
Always compare the returned state against the value you stored before the popup opened. Skipping this check leaves the flow open to CSRF.

3. Exchange the Code

Send the code and the verifier from your backend, authenticating with HTTP Basic.
Response
Store both tokens against your user record. Authorization codes are single use and expire after 10 minutes, and presenting one a second time will revoke every token it issued.

4. Confirm the Connection

Call GET /partner/v1/me with the new access token.
cURL
The scopes array in this response reflects what the token can actually do, which may differ from what you requested at authorize time. We recommend treating it as the source of truth.

Mobile Fallback

Popups are unreliable on mobile browsers. You can run the same flow by navigating the whole page instead of opening a window, then reading code from the query string server-side on your callback route. Nothing else about the flow changes.

1-Click Trading

The trade:execute and governance write scopes require the user to have delegated their wallet to Trench. The consent screen enforces this, so if delegation is off, the Approve button stays disabled until the user enables it. This means you will never be issued a trading token that cannot trade at the time it is created. A user can revoke delegation later, though, so we recommend checking tradingEnabled before showing trade controls. Once delegation is off, writes fail with 403 delegation_missing.

Next Steps

Trade Tokens

Execute your first buy and confirm the fill.

Tokens & Refresh

Keep the connection alive without breaking it.