Iām integrating the Trading 212 API into a Next.js app using HTTP Basic Authentication. Fresh ISA API credentials are being rejected with āInvalid API keyā error, even though the key shows as Active in my account settings. Hereās my implementationā¦"
- code structure (without credentials):
typescript
function getBasicAuthHeader(): string {
const apiKey = process.env.TRADING212_API_KEY;
const secretKey = process.env.TRADING212_SECRET_KEY;
const credentials = `${apiKey}:${secretKey}`;
const encodedCredentials = Buffer.from(credentials).toString('base64');
return `Basic ${encodedCredentials}`;
}
- The curl command Iām using (with redacted credentials):
bash
curl -X GET "https://live.trading212.com/api/v0/equity/account/cash" \
-H "Authorization: Basic [BASE64_ENCODED_CREDENTIALS]"
- The error response:
json
{"code":"UNAUTHENTICATED","message":"Invalid API key."}
Any ideas on how to move this forward?