Skip to content

Authentication

The Quantum Trader API uses JWT (JSON Web Tokens) for authentication. Access tokens expire after 30 minutes, refresh tokens after 30 days.

┌─────────┐ ┌─────────┐ ┌─────────┐
│ Login │ ──► │ Tokens │ ──► │ API │
│ POST │ │ Stored │ │ Calls │
└─────────┘ └─────────┘ └─────────┘
┌─────────────┐
│ Refresh │ (when access token expires)
│ Tokens │
└─────────────┘
MethodEndpointAuth RequiredDescription
POST/auth/loginNoLogin and get tokens
POST/auth/refreshNoRefresh access token
POST/auth/logoutYesRevoke refresh token
GET/auth/meYesGet current user info

Authenticate with username/email and password to receive JWT tokens.

POST /api/v1/auth/login
Content-Type: application/json
{
"username": "[email protected]",
"password": "your_password"
}
FieldTypeRequiredDescription
usernamestringYesUsername or email address
passwordstringYesAccount password
FieldTypeDescription
access_tokenstringJWT for API authentication (30 min expiry)
refresh_tokenstringJWT for refreshing access token (30 day expiry)
token_typestringAlways "bearer"
StatusCauseResponse
401Invalid credentials{"detail": "Incorrect username or password"}
403Account disabled{"detail": "User account is disabled"}
const login = async (username, password) => {
const response = await fetch('http://localhost:8501/api/v1/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail);
}
const tokens = await response.json();
// Store tokens securely
localStorage.setItem('access_token', tokens.access_token);
localStorage.setItem('refresh_token', tokens.refresh_token);
return tokens;
};
// Usage
try {
const tokens = await login('[email protected]', 'password123');
console.log('Logged in successfully');
} catch (error) {
console.error('Login failed:', error.message);
}

Get a new access token using your refresh token. The old refresh token is invalidated (token rotation).

POST /api/v1/auth/refresh
Content-Type: application/json
{
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
const refreshTokens = async () => {
const refreshToken = localStorage.getItem('refresh_token');
const response = await fetch('http://localhost:8501/api/v1/auth/refresh', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh_token: refreshToken })
});
if (!response.ok) {
// Refresh token invalid/expired - redirect to login
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = '/login';
throw new Error('Session expired');
}
const tokens = await response.json();
localStorage.setItem('access_token', tokens.access_token);
localStorage.setItem('refresh_token', tokens.refresh_token);
return tokens;
};
// Auto-refresh interceptor with Axios
api.interceptors.response.use(
response => response,
async error => {
if (error.response?.status === 401) {
try {
const tokens = await refreshTokens();
error.config.headers['Authorization'] = `Bearer ${tokens.access_token}`;
return api.request(error.config);
} catch (refreshError) {
return Promise.reject(refreshError);
}
}
return Promise.reject(error);
}
);

Revoke the refresh token to end the session.

POST /api/v1/auth/logout
Authorization: Bearer <access_token>
Content-Type: application/json
{
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Retrieve the authenticated user’s profile information.

GET /api/v1/auth/me
Authorization: Bearer <access_token>
FieldTypeDescription
idUUIDUnique user identifier
emailstringUser’s email address
usernamestringUser’s username
full_namestringUser’s display name
rolestringOne of: user, premium, admin
is_activebooleanAccount active status
is_verifiedbooleanEmail verification status
created_atdatetimeAccount creation timestamp
last_login_atdatetimeLast login timestamp

Include the access token in the Authorization header for all authenticated requests:

GET /api/v1/strategies
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

RolePermissions
userStandard access - own strategies, backtests, portfolios
premiumExtended data access, priority task execution
adminFull access - user management, system configuration

Secure Storage

Store tokens in HttpOnly cookies or secure storage, never in plain localStorage for production.

Token Refresh

Refresh tokens 5 minutes before expiry to prevent interruption.

Logout Properly

Always call the logout endpoint to invalidate refresh tokens on the server.

HTTPS Only

Always use HTTPS in production to protect tokens in transit.