Skip to main content

Authentication

The MagFi API uses bearer token authentication. You can authenticate using either a short-lived JWT token or a persistent API key.

Choosing an Authentication Method

Use CaseMethodToken Lifespan
Server-to-server integrationsAPI KeyPermanent (until revoked)
User-facing applicationsJWT Token1 hour
Testing & developmentEither-

Environment Setup

All code examples in this guide use a BASE_URL variable. Set this to your target environment:

  • Production: https://api.magfi.net
  • Sandbox: https://api.sandbox.magfi.dev

JWT Token Authentication

JWT tokens are ideal for user-facing applications where you authenticate as a specific user. Tokens expire after 1 hour.

Steps

  1. Call POST /auth/login with email and password
  2. Receive a token in the response
  3. Include token in Authorization: Bearer <token> header for subsequent requests
  4. Request a new token when the current one expires (after 1 hour)

Example: Login and Get Token

BASE_URL="https://api.magfi.net"

# Step 1: Login to get JWT token
curl -X POST $BASE_URL/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "your-password"
}'

# Response:
# {
# "token": "<your-jwt-token>"
# }

Example: Using the JWT Token

# Step 2: Use the JWT token in subsequent requests
TOKEN="<your-jwt-token>"

curl -X GET $BASE_URL/accounts \
-H "Authorization: Bearer $TOKEN"

See also: POST /auth/login | POST /auth/logout


API Key Authentication

API keys are ideal for server-to-server integrations and persistent access without user interaction. API keys do not expire unless revoked.

Steps

  1. Authenticate with a JWT token first (required to create API keys)
  2. Call POST /auth/api-key with your JWT bearer token
  3. Save the returned API key immediately - it's only shown once!
  4. Use the API key in Authorization: Bearer <api-key> header for all future requests

Example: Create an API Key

# Step 1: Create an API key (requires JWT token)
JWT_TOKEN="<your-jwt-token>"

curl -X POST $BASE_URL/auth/api-key \
-H "Authorization: Bearer $JWT_TOKEN"

# Response:
# {
# "id": "550e8400-e29b-41d4-a716-446655440000",
# "apiKey": "magfi_YOUR_API_KEY_HERE"
# }
#
# ⚠️ IMPORTANT: Save this API key now - it will not be shown again!

Example: Using the API Key

# Step 2: Use the API key in subsequent requests
API_KEY="magfi_YOUR_API_KEY_HERE"

curl -X GET $BASE_URL/accounts \
-H "Authorization: Bearer $API_KEY"

Managing API Keys

List all your API keys: GET /auth/api-key

Delete an API key: DELETE /auth/api-key/:id

See also: POST /auth/api-key


Troubleshooting

IssueCauseSolution
401 UnauthorizedMissing or invalid tokenVerify token is included in Authorization: Bearer <token> header
401 UnauthorizedExpired JWT tokenJWT tokens expire after 1 hour. Request a new token via /auth/login
401 UnauthorizedInvalid credentialsCheck email/password are correct
403 ForbiddenValid token, insufficient permissionsYour account doesn't have access to this resource
API key not workingKey was deleted or never savedAPI keys are shown only once. If lost, create a new one
Network errorsConnection issues or invalid BASE_URLVerify you're using the correct environment URL (production or sandbox)

Security Reminders

  • 🔒 Store API keys securely - Never commit them to version control. Use environment variables.
  • 🔒 Use HTTPS only - The API enforces HTTPS, but ensure your client does too.
  • 🔒 Rotate keys regularly - Delete old API keys you're no longer using via DELETE /auth/api-key/:id.