Skip to main content

A Python library for the unofficial Avanza API

Project description

Avanza

A Python library for the unofficial Avanza API. This library is based on the existing JavaScript library Avanza.

Please note that this is only a proof of concept, hence not meant to be used by anyone.

It might also be valuable to note that I am not affiliated with Avanza Bank AB in any way. The underlying API can be taken down or changed without warning at any point in time.

Installation

pip install avanza-api

Getting a TOTP Secret

NOTE: Since May 2018 two-factor authentication is required to log in.

Here are the steps to get your TOTP Secret:

  1. 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.)
  2. Click "Aktivera" on the next screen.
  3. Select "Annan app för tvåfaktorsinloggning".
  4. Click "Kan du inte scanna QR-koden?" to reveal your TOTP Secret.
  5. Generate the TOTP code using the python code below and paste the TOTP code in the field below where you found the TOTP Secret.
  6. Done! From now on all you have to do is supply your secret in the constructor as in the examples below.

Generate TOTP code:

import hashlib
import pyotp
totp = pyotp.TOTP('MY_TOTP_SECRET', digest=hashlib.sha1)
print(totp.now())

Example

Authenticate and fetch account overview:

from avanza import Avanza
avanza = Avanza({
    'username': 'MY_USERNAME',
    'password': 'MY_PASSWORD',
    'totpSecret': 'MY_TOTP_SECRET'
})

overview = avanza.get_overview()

Get info about a certain account

from avanza import Avanza, TimePeriod

avanza = Avanza({
    'username': 'MY_USERNAME',
    'password': 'MY_PASSWORD',
    'totpSecret': 'MY_TOTP_SECRET'
})

report = avanza.get_insights_report(
    account_id='XXXXXXX',
    time_period=TimePeriod.ONE_WEEK
)

Place an order

from avanza import Avanza, OrderType

avanza = Avanza({
    'username': 'MY_USERNAME',
    'password': 'MY_PASSWORD',
    'totpSecret': 'MY_TOTP_SECRET'
})

result = avanza.place_order(
    account_id='XXXXXXX',
    order_book_id='XXXXXX',
    order_type=OrderType.BUY,
    price=13.37,
    valid_until=date.fromisoformat('2011-11-11'),
    volume=42
)

Subscribe to real time data

import asyncio
from avanza import Avanza, ChannelType

def callback(data):
    # Do something with the quotes data here
    print(data)

async def subscribe_to_channel(avanza: Avanza):
    await avanza.subscribe_to_id(
        ChannelType.QUOTES,
        "19002", # OMX Stockholm 30
        callback
    )

def main():
    avanza = Avanza({
        'username': 'MY_USERNAME',
        'password': 'MY_PASSWORD',
        'totpSecret': 'MY_TOTP_SECRET'
    })

    asyncio.get_event_loop().run_until_complete(
        subscribe_to_channel(avanza)
    )
    asyncio.get_event_loop().run_forever()

if __name__ == "__main__":
    main()

Testing

Tests are stored in /tests

The tests are using Pydanctic models which are used to validate that the response model is what's expected

There are tests that call all available GET endpoints and validates that the response model of these endpoints are correct and that the endpoint still exists

To run the tests you first need to create a .env file and have the following keys in it:

USERNAME=
PASSWORD=
TOTP_SECRET=
ACCOUNT_ID=
PRICE_ALERT_ORDER_BOOK_ID=

Then you can do one of the following:

  • To run all tests: python -m unittest.
  • To run a single test, such as test_overview: python3 -m unittest tests.test_endpoints.ReturnModelTest.test_overview.

Extending/updating the API

Suppose we want to add a new method get_foo_bar to the API defined by avanza.py along with a test that both

  • shows that the request sent by our new method yields a successful response from the server, and
  • starts failing whenever Avanza makes breaking changes (attribute removal or data type changes) to their API.

The steps are then roughly these:

  1. Add the method to avanza.py, making it call a URI using a route that you add to constants.py.
  2. Add a simple test to tests/test_endpoints.py which calls the method using the get_or_cache wrapper function (but does not yet validate the JSON response received).
  3. Run the test (see Testing above). As part of running the test, the said wrapper function will generate the file get_foo_bar.json.
  4. Generate Pydantic models corresponding to the JSON response using the tool datamodel-code-generator:
    1. Install the tool into your virtual environment using pip install datamodel-code-generator.
    2. Run
      datamodel-codegen --class-name=FooBar --enable-version-header --target-python-version=3.9 \
        --input get_foo_bar.json --input-file-type=json \
        --output avanza/models/foo_bar.py --output-model-type=pydantic_v2.BaseModel
      
      to generate a tree of models corresponding to the JSON response to the module avanza/models/foo_bar.py. Note: The --target-python-version should match whatever version REQUIRES_PYTHON in setup.py indicates.
    3. Add the necessary import to avanza/models/__init__.py.
  5. Update the test to validate the JSON response against the newly created model by adding the appropriate model_validate call. This way, the test will fail if Avanza updates their API (by default, attribute removals and data type changes will cause a test failure, but not addition of new attributes).

License

MIT license. See the LICENSE file for details.

Responsibilities

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

avanza_api-15.1.1.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

avanza_api-15.1.1-py3-none-any.whl (35.1 kB view details)

Uploaded Python 3

File details

Details for the file avanza_api-15.1.1.tar.gz.

File metadata

  • Download URL: avanza_api-15.1.1.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for avanza_api-15.1.1.tar.gz
Algorithm Hash digest
SHA256 e65ddeeaf984ce73e019408e64424f7901c41904cc17d8c74fa6e01f6d5288f6
MD5 06ce83bac1945c73025b8b83f728d48f
BLAKE2b-256 56cafbe02ccbc127a74ff485a1703b652673d561fe77b8ad156e8f196b092f15

See more details on using hashes here.

Provenance

The following attestation bundles were made for avanza_api-15.1.1.tar.gz:

Publisher: python-publish.yml on Qluxzz/avanza

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file avanza_api-15.1.1-py3-none-any.whl.

File metadata

  • Download URL: avanza_api-15.1.1-py3-none-any.whl
  • Upload date:
  • Size: 35.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for avanza_api-15.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07d6b3b7ca190e663f5a2768603b0a984e1fbc44e318d3c4e0b7e4807dfc4b3a
MD5 d87594c4da6cd1bb8e812bff53efa38d
BLAKE2b-256 2bb18c9601a5195d0f88e26581e8d205deefc538372f6fd94ab02339bd4c939e

See more details on using hashes here.

Provenance

The following attestation bundles were made for avanza_api-15.1.1-py3-none-any.whl:

Publisher: python-publish.yml on Qluxzz/avanza

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page