A Kotlin port of the Node.js lib wrapping the unofficial Avanza API, https://github.com/fhqvst/avanza. As with the original this is a proof of concept, made to practice Kotlin and not for serious use. You can use it from Java (or any suitable JVM language with good interoperability) with Java interop making it the perfect Java Avanza API.
I am not affiliated with Avanza and the API might change in any way at any time, rendering this library unusable.
The API has changed a bit and there has been some code changes to accommodate this, but the fixes are not complete and some methods have been deprecated for later consideration and some have been indefinitely deprecated.
-
getChartData -
getInstrument -
getInspirationLists
-
getOrder(you can still get all orders throughgetDealsAndOrders)
In order to log in to Avanza, you need to enable two-factor authentication and establish a TOTP secret.
You will need to confirm the TOTP secret later. You can do that with an authenticator app, but I have also included a jar for this purpose. Depending how much you trust me you can use it straight from the repo or inspect the code first and build it yourself.
Great! Just use the totpcodegen-1.0.jar from the root of this repo.
Sigh. Code is in totpcodegen
cd totpcodegen
./gradlew build
ls build/libs/totpcodegen-1.0.jarOn the Avanza web page, log in and:
- Go to Mina Sidor > Profil > Sajtinställningar > Tvåfaktorsinloggning and click Återaktivera
- Click Ja, återaktivera
- Click Aktivera
- Under Annan app för tvåfaktorsinloggning click Nu har jag laddat ner appen, gå vidare
- Enter your password
- Click Kan du inte scanna QR-koden? to reveal the TOTP secret, 32 character long base 32 string.
- Confirm Secret by generating a TOTP code, perhaps with the totpcodegen-1.0.jar
java -jar totpcodegen-1.0.jar <YOUR_TOTP_SECRET_HERE>
- Save your secret somewhere secret.
Gradle/Maven
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.widarlein:kavanza:1.0'
}By calling Avanza.connect() with proper arguments, the lib will log you in and return a client for further interactions.
Check out all capabilities of the AvanzaClient by checking the docs or reading the code.
import com.github.widarlein.kavanza.Avanza
import com.github.widarlein.kavanza.AvanzaClient
import com.github.widarlein.kavanza.model.*
val avanzaClient: AvanzaClient = Avanza.connect(UserCredentials("<USERNAME>", "<PASSWORD>"), "<TOTP_SECRET>")
val overview = avanzaClient.getOverview()
println("User account overview: $overview")You find the dokka docs here, baby https://widarlein.github.io/kavanza/
AvanzaClient.getInspirationLists()does not currently work and will cast an exception if you try to use it. The response json contains some polymorphism depending on what type of list is returned and I haven't worked out a good way of handling that yet. The only type actually working is STOCK, but this endpoint can return several different types.AvanzaClient.getInspirationList()has the same problem as above. Only type responses of type STOCK would work, but the only types available to request are different lists with type FUND.- Real time data is not yet implemented.
Use at own risk, I'm not responsible for anything you do with this.