A TypeScript/JavaScript client library for authenticating with the Hermes Business Customer Authentication API, implementing OAuth 2.0 with support for Password Grant, Authorization Code Grant, and Refresh Token flows.
bun install- OAuth 2.0 Grant Types:
- Password Grant
- Authorization Code Grant
- Refresh Token Grant
- Automatic Token Management:
- Token refresh on expiry
- Configurable expiry buffer
- Persistent token storage (browser)
- API Client with Auth Middleware:
- Automatic Authorization header injection
- Automatic token refresh and request retry
- JWT Token Utilities:
- Token decoding
- Expiry checking
- User info extraction
import { HermesAuthClient, HermesApiClient } from 'hermes-api';
const authClient = new HermesAuthClient({
clientId: 'your-client-id',
clientSecret: 'your-client-secret', // Optional
authBaseUrl: 'https://authme.myhermes.de', // Production
});
// Authenticate
const tokenResponse = await authClient.authenticateWithPassword(
'username',
'password'
);
// Create API client for authenticated requests
const apiClient = new HermesApiClient(
authClient,
'https://your-api.myhermes.de'
);
// Make authenticated API calls
const data = await apiClient.get('/api/endpoint');// Step 1: Get authorization URL
const authUrl = authClient.getAuthorizationUrl(
'https://your-app.com/callback',
'optional-state-string'
);
// Redirect user to authUrl
// Step 2: Exchange code for tokens
const tokenResponse = await authClient.exchangeAuthorizationCode(
'authorization-code-from-redirect',
'https://your-app.com/callback'
);The API client automatically handles token refresh:
// Token will be refreshed automatically if expired
const data = await apiClient.get('/api/protected-endpoint');Track Hermes shipments using the HSI API:
import { HermesShipmentApi } from 'hermes-api';
// Create shipment API instance
const shipmentApi = new HermesShipmentApi(apiClient);
// Get shipment information
const shipmentInfo = await shipmentApi.getShipmentInfo('1234567890123456');
console.log('Status:', shipmentInfo.statusLangText);
console.log('Expected Delivery:', shipmentInfo.voraussichtlicherZustelltag);
// Check if delivered
const isDelivered = await shipmentApi.isShipmentDelivered('1234567890123456');
// Get tracking history
const history = await shipmentApi.getTrackingHistory('1234567890123456');
// Get proof of delivery
const pod = await shipmentApi.getProofOfDelivery('1234567890123456');authenticateWithPassword(username, password)- Password grant authenticationexchangeAuthorizationCode(code, redirectUri)- Exchange auth code for tokensrefreshAccessToken(refreshToken?)- Refresh access tokengetAuthorizationUrl(redirectUri, state?)- Get OAuth authorization URLgetAccessToken()- Get current access token (auto-refresh if expired)getAuthorizationHeader()- Get Bearer token header valueisAuthenticated()- Check if authenticated with valid tokenlogout()- Clear stored tokens
get(endpoint, options?)- GET requestpost(endpoint, data?, options?)- POST requestput(endpoint, data?, options?)- PUT requestdelete(endpoint, options?)- DELETE requestpatch(endpoint, data?, options?)- PATCH request
getShipmentInfo(trackingNumber)- Get complete shipment informationgetMultipleShipmentInfo(trackingNumbers[])- Get info for multiple shipmentsgetShipmentStatus(trackingNumber)- Get current status textisShipmentDelivered(trackingNumber)- Check if deliveredgetEstimatedDeliveryDate(trackingNumber)- Get expected delivery dategetTrackingHistory(trackingNumber)- Get full tracking historygetProofOfDelivery(trackingNumber)- Get proof of delivery detailsgetRecipientInfo(trackingNumber)- Get recipient informationgetEcoInfo(trackingNumber)- Get ecological impact informationgetReturnInfo(trackingNumber)- Get return label information
saveToken(token)- Save token to localStoragegetToken()- Retrieve saved tokenclearToken()- Remove saved tokendecodeToken(token)- Decode JWT without verificationisTokenExpired(token)- Check token expirygetTokenTimeToLive(token)- Get seconds until expiryextractUserInfo(token)- Extract user claims from token
- Production:
https://authme.myhermes.de - Integration:
https://authme-int.myhermes.de
- Access tokens are valid for 1 hour
- Tokens are JWT format with max size of 8KB
- Automatic refresh occurs 5 minutes before expiry
examples/usage.ts- Authentication and API usage examplesexamples/shipment-tracking.ts- Shipment tracking examples
This project uses Bun runtime.
# Install dependencies
bun install
# Run examples
bun run examples/usage.ts