A Node.js wrapper for the unofficial Avanza API. Created as most of the libraries out there are outdated or not maintained. We will do our best to keep this up to date, but please create issues if you find any bugs or have feature requests on our GitHub page.
Install via npm
$ npm install avanza-api-unofficial
Refer to API.md for complete API documentation. The documentation is generated from JSDoc comments using jsdoc-to-markdown.
Here are the steps to get your TOTP Secret:
- Go to Profil > Inställningar > Sajtinställningar > Inloggning och utloggning > Användarnamn > Tvåfaktorsinloggning and click "Återaktivera". (Only do this step if you have already set up two-factor auth.)
- Click "Aktivera" on the next screen.
- Select "Annan app för tvåfaktorsinloggning".
- Click "Kan du inte scanna QR-koden?" to reveal your TOTP Secret.
- Finally, run
node -e "console.log(require('avanza-api-unofficial/dist/totp')('PASTE_YOUR_TOTP_SECRET_HERE'))"
to generate an initial code. - Done! From now on all you have to do is supply your secret in the
authenticate()
function as in the example below.
Authenticate and fetch currently held positions:
import Avanza from 'avanza-api-unofficial'
const avanza = new Avanza()
avanza.authenticate({
username: 'MY_USERNAME',
password: 'MY_PASSWORD',
totpSecret: 'MY_TOTP_SECRET'
}).then(async () => {
const positions = await avanza.getPositions()
console.log(positions)
})
Authenticate and subscribe to real-time data:
import Avanza from 'avanza-api-unofficial'
const avanza = new Avanza()
avanza.authenticate({
username: 'USERNAME',
password: 'PASSWORD',
totpSecret: 'MY_TOTP_SECRET'
}).then(() => {
avanza.subscribe(Avanza.QUOTES, '5479', (quote) => {
console.log('Received quote:', quote)
})
})
Refer to API.md.
This project contains comprehensive tests for all API methods. Tests are run automatically:
- Before any commit (via pre-commit hook)
- During the release process
Tests will run with or without credentials. If you don't provide credentials in a .env
file, the API call tests will be skipped, but the path construction tests will still run.
- Copy
.env.example
to.env
and fill in your credentials - Run the tests:
# Run all tests
$ npm test
# Run tests with coverage report
$ npm run test:coverage
- Path Tests: Verify all API endpoint paths are constructed correctly (runs without credentials)
- API Tests: Verify actual API calls work correctly (requires credentials)
- No Real Trades: The tests are designed to check functionality without placing any actual orders
The GitHub Actions workflow runs the path tests automatically without requiring credentials.
This library includes a tool to help detect changes in Avanza's API endpoints, which can change without notice.
The endpoint crawler logs into Avanza's web interface using Puppeteer and monitors network traffic to detect API endpoints. This helps discover when endpoints have changed (e.g., from /_mobile/
to /_api/
paths).
# Install required dependencies if not already installed
npm install
# Run the endpoint crawler
npm run endpoint-crawler
# Run with comparison against current constants.js
npm run check-endpoints
See ENDPOINT_CRAWLER.md for detailed documentation on using the crawler and keeping the API up to date.
MIT license. See the LICENSE file for details.
The author of this software is not responsible for any indirect damages (foreseeable or unforeseeable), such as, if necessary, loss or alteration of or fraudulent access to data, accidental transmission of viruses or of any other harmful element, loss of profits or opportunities, the cost of replacement goods and services or the attitude and behavior of a third party.