OAuth 2.0

Let an Envia user authorize your application without them pasting an API token into it.

You need this if you are building something other Envia companies will install — a marketplace app, a WMS integration, an agent. A marketplace that lets each seller use their own Envia account is the typical case: see Marketplace Multi-Seller Shipping. For your own account you do not need any of this: take the API token from the dashboard and see Authentication & Environments.

This is a standard OAuth 2.0 authorization server. If you already have an OAuth client library, point it at the metadata document and it will configure itself:

curl https://queries.test.envia.com/.well-known/oauth-authorization-server

The flow in one minute

  1. Register your application once, and keep its client_id.
  2. Send the user to Envia. They sign in, see what you ask for, and approve.
  3. Exchange the code that comes back for an access token and a refresh token.
  4. Call the API with the access token.
  5. Refresh before the access token expires, and revoke when the user disconnects.
Grantsauthorization_code and refresh_token for every client. client_credentials only for clients Envia creates — see Limits
PKCERequired, and only S256. A request without code_challenge is rejected
Response typescode
Client authnone for registered clients. client_secret_basic and client_secret_post for clients Envia creates
RegistrationOpen — RFC 7591 dynamic client registration
Access tokenBearer JWT, valid for 1 hour (expires_in: 3600)
Refresh tokenValid for 30 days from the approval. Every refresh returns a new one
IdentityNone. There is no id_token and no userinfo endpoint

Everything lives on the Management API host. The examples on this page use the test host, queries.test.envia.com. A client registered on the test host does not exist on queries.envia.com, so run the whole flow on one host.

The endpoints

OperationEndpointToken needed
MetadataGET /.well-known/oauth-authorization-serverno
Register a clientPOST /oauth/v2/registerno
Start authorizationGET /oauth/v2/authorizeno
Exchange for a tokenPOST /oauth/v2/tokenno
Revoke a tokenPOST /oauth/v2/revokeno
Inspect a tokenPOST /oauth/v2/introspectyes

Each one is rate limited on its own, per IP address: 10 registrations a minute, 30 authorizations, 20 token calls and 20 revocations.

Step 1 — Register your client

API reference → Register an OAuth client

curl -X POST https://queries.test.envia.com/oauth/v2/register \
  -H 'Content-Type: application/json' \
  -d '{
    "client_name": "Acme WMS",
    "redirect_uris": ["https://acme.example.com/envia/callback"],
    "scope": "shipments:read shipments:write rates:read"
  }'

The response carries your client_id. Save it: you send it in every later step.

{
  "client_id": "7d1f6c2e-3b8a-4f0e-9c55-2a4b8e1d9f10",
  "client_id_issued_at": 1795800000,
  "client_name": "Acme WMS",
  "redirect_uris": ["https://acme.example.com/envia/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "token_endpoint_auth_method": "none",
  "scope": "shipments:read shipments:write rates:read"
}

client_name and redirect_uris are required. Register up to ten URIs. client_uri, logo_uri, contacts and description are optional and are what the user sees on the consent screen — fill them in.

Open registration always creates a public client. token_endpoint_auth_method can only be none, so there is no client secret and PKCE is what proves the request is yours. That is the right shape for anything that runs on a device you do not control.

A client with a secret is created by Envia, not by this endpoint. Ask support.

Redirect URIs

AcceptedRejected with invalid_redirect_uri
https://…http://localhost… and any other http host
http://127.0.0.1:<port>/… and http://[::1]:<port>/…, for an app on the user's own deviceCustom schemes such as myapp:// or cursor://
Any URI with a fragment (#…)

The redirect_uri you send in Step 2 and Step 3 must match a registered one exactly, port included. A bare origin matches with or without the trailing slash; a path does not.

Scopes at registration

Send the scopes you need, separated by spaces. Without scope you get mcp:read. Read the scope in the response: a scope that does not exist, or that is not open to registered clients, is dropped with no error. See The scopes.

grant_types is optional and can list authorization_code and refresh_token. Asking for client_credentials returns 400 invalid_client_metadata.

A client that no user authorizes within 7 days of registration is deactivated. Register again if yours was.

A bad payload comes back in the OAuth error shape, with invalid_redirect_uri when the problem is in redirect_uris:

{ "error": "invalid_client_metadata",
  "error_description": "\"client_name\" is required" }

Step 2 — Send the user to authorize

API reference → Start authorization

Generate a random code_verifier of 43 to 128 characters, and keep it. Send base64url(sha256(verifier)) as the challenge.

GET https://queries.test.envia.com/oauth/v2/authorize
  ?response_type=code
  &client_id=<your client_id>
  &redirect_uri=https://acme.example.com/envia/callback
  &code_challenge=<base64url(sha256(verifier))>
  &code_challenge_method=S256
  &scope=shipments:read shipments:write
  &state=<your anti-CSRF value>

response_type, client_id, redirect_uri, code_challenge and code_challenge_method are all required. The method must be S256 — plain is not accepted. state is optional, up to 4,096 characters; send it and check it when the user comes back.

The user signs in with their Envia account, sees your application's name, the permissions you ask for and the host of your redirect URI, and approves or denies. They can approve fewer permissions than you asked for, never more.

Envia then redirects to your redirect_uri:

OutcomeWhat you receive
Approved?code=<code>&state=<your state>. The code works once and expires in 10 minutes
Denied?error=access_denied&state=<your state>
Scope not allowed to your client?error=invalid_scope&error_description=…
resource not accepted?error=invalid_target

Do not send resource. It accepts only Envia's own MCP server addresses; anything else is invalid_target.

Step 3 — Exchange the code for a token

API reference → Exchange a code, or refresh

curl -X POST https://queries.test.envia.com/oauth/v2/token \
  -H 'Content-Type: application/json' \
  -d '{
    "grant_type": "authorization_code",
    "code": "<the code from the redirect>",
    "redirect_uri": "https://acme.example.com/envia/callback",
    "code_verifier": "<the original verifier>",
    "client_id": "<your client_id>"
  }'

JSON or application/x-www-form-urlencoded — both are accepted.

{
  "access_token": "<JWT>",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "<64 hex characters>",
  "scope": "shipments:read shipments:write"
}

Store the refresh token the way you would store a password. The scope here is what the user approved: use it, not what you asked for.

Fields that belong to another grant are rejected, not ignored. code, redirect_uri and code_verifier are required for authorization_code and forbidden for the others. Sending code with grant_type: "refresh_token" is a 400.

Use each code once. Presenting a code a second time revokes every token your application holds for that user, and the user has to approve again.

Step 4 — Call the API

Send the access token as a Bearer token, like any other Envia token:

curl https://queries.test.envia.com/shipments?limit=1 \
  -H "Authorization: Bearer $ACCESS_TOKEN"

Three things decide whether a call works:

  • The token's scopes must cover the path. A call outside them answers 403 with the scope you are missing:

    { "error": "insufficient_scope",
      "error_description": "Missing scope shipments:write",
      "scope": "shipments:write" }
  • The user's role still applies. A token never does more than the user could do in the dashboard.

  • The company: a token works for one company, the one the user approved for. Labels you buy with it are charged to that company's balance, and a cancellation refunds that balance.

A path that no scope covers answers 403 insufficient_scope with Path is not permitted for OAuth access tokens, whatever scopes you hold.

On the Shipping API

The same access token works on the Shipping API (api-test.envia.com in sandbox, api.envia.com in production), for quotes, labels, tracking, pickups and the other /ship/* calls.

  • Creating a label needs shipments:write, and canceling needs shipments:cancel. Both answer the same 403 insufficient_scope body as above.
  • /v1/ship/* does not accept OAuth tokens. Use the account's API token there.
  • A token that is invalid, expired or revoked answers 401 with the text Authentication error., not JSON.
  • Revoking stops a token on the Management API at once. On the Shipping API an access token you already revoked keeps working until it expires, up to one hour. The refresh token stops at once everywhere.

The scopes

Nineteen permissions, read and write separated, and two bundles that expand into several of them:

ScopeOpens
shipments:read · shipments:write/shipments, /ship (create labels), /guide
shipments:cancel/ship/cancel. Separate on purpose: canceling refunds money
rates:read/ship/rate, /rate, /available-carrier, /additional-services, /service
labels:read/label
orders:read · orders:write/orders, /v2/orders, /v4/orders
pickups:read · pickups:write/pickup, /pickup-limits, /branches
addresses:read · addresses:write/address, /clients, /generic-form
packages:read · packages:write/package
tickets:read · tickets:write/tickets, /company/tickets
company:read/company, /user-information, /products, /reports, /analytics, /notifications, /config, /webhooks
BundleExpands to
mcp:readEvery :read scope above, except billing:read
mcp:shipmcp:read, plus shipments:write, shipments:cancel, pickups:write, addresses:write, packages:write and orders:write

Ask for the least you need. A user who is happy to let you create shipments may not want you canceling them. A marketplace that quotes, buys, tracks and cancels labels for its sellers needs rates:read shipments:read shipments:write shipments:cancel.

company:write, billing:read and billing:write exist but are not available to any client that sends the user through Step 2. Ask for them and they are dropped from scope. To change webhooks, shipping configuration or checkout rules, or to read billing, use the account's API token.

Refresh the token

curl -X POST https://queries.test.envia.com/oauth/v2/token \
  -H 'Content-Type: application/json' \
  -d '{
    "grant_type": "refresh_token",
    "refresh_token": "<your refresh token>",
    "client_id": "<your client_id>"
  }'

Send nothing else from Step 3's list. Add scope only to ask for fewer permissions than the token has; the narrower set then stays on every later refresh.

  • Every refresh returns a new refresh token, and the one you sent stops working. Save the new one before you use the new access token.
  • Never refresh twice at once with the same token. The second call looks like a replay: both tokens are revoked and the user has to approve again.
  • The 30 days do not restart. After 30 days from the approval the user approves again, however often you refresh.

Giving a token back

curl -X POST https://queries.test.envia.com/oauth/v2/revoke \
  -H 'Content-Type: application/json' \
  -d '{"token": "<access or refresh token>", "client_id": "<your client_id>"}'

Revoke when a user disconnects your app. It is one call and it is the difference between an uninstall and a token that still works. The call answers 200 with an empty body, including for a token that does not exist or belongs to another client.

When something fails

Authorization errors, token errors and API errors do not all come in the same shape.

WhereShape
POST /oauth/v2/register, and an unknown client_id or unregistered redirect_uri on /oauth/v2/authorizeOAuth: { "error": "…", "error_description": "…" }
A malformed request to /oauth/v2/authorize or /oauth/v2/tokenManagement API: { "statusCode": 400, "error": "Bad Request", "message": "…" }
Every other failure on /oauth/v2/tokenManagement API, with the reason in message
A call with an access token that lacks a scopeOAuth: { "error": "insufficient_scope", … } with status 403

/oauth/v2/token never answers invalid_grant or invalid_request. A standards-compliant client library reads error and gets "Bad Request", so read message:

messageStatusWhat to do
Invalid authorization code, Authorization code already used, Authorization code expired400Start again at Step 2
redirect_uri mismatch400Send the same redirect_uri you used in Step 2
Invalid code_verifier400Send the verifier that produced the challenge
Invalid refresh token, Refresh token expired, Refresh token has been revoked400Send the user through Step 2
invalid_scope400On refresh, you asked for a scope the token does not have
invalid_target400Remove resource
unauthorized_grant_type400Your client does not hold that grant
Invalid client_id, Invalid client credentials, Public clients must not send client_secret401Check the client authentication from Step 1

A 503 from /oauth/v2/token or /oauth/v2/revoke leaves the token untouched: send the same request again.

Limits to plan for

If you plan toWhat happensWhat to do
Run on a desktop or mobile app with a custom scheme, or on http://localhostRegistration answers 400 invalid_redirect_uriUse https, or http://127.0.0.1 with a fixed port
Pick a random free port on each launchThe redirect_uri does not match and /oauth/v2/authorize answers 400Register the port you will use
Change webhooks, shipping rules or checkout rules, or read or change billingThe scope is dropped, and the call answers 403 insufficient_scopeUse the account's API token for those calls
Call a path no scope covers403 insufficient_scopeUse the table in The scopes to plan
Use client_credentials for server-to-server callsEnvia creates the client. The token has no user and no company, and the API rejects itUse the account's API token
Verify the access token yourselfIt is signed with a secret Envia keeps, and there is no JWKS endpointTreat it as opaque and send it to the API
Introspect a tokenA self-registered client gets 401Introspect with a client Envia creates, or skip it
Ask for an audience with resourceOnly Envia's MCP server addresses are acceptedOmit resource
Cut a user's access the moment they disconnectThe Shipping API accepts a revoked access token until it expires, up to one hourRevoke, then stop using the token on your side
Call /v1/ship/* with the token401Use the account's API token
Keep a user connected for longer than 30 daysThe refresh token stops working on day 30Ask the user to approve again

The older redirect flow

You may find an older integration using https://ship.envia.com/envios/oauth?redirect=…, which handed a token straight to a redirect URL. It still exists. Do not build anything new on it: it has no scopes, no PKCE, no refresh and no revocation, so a token it issues can do everything the user can do, for as long as it lives.

Testing in sandbox
  • Registrations are real writes on the test host. Register once and reuse the client_id while you build.
  • Production has its own client registry: register again on queries.envia.com when you go live.