degiro-api

repository·master·Indexed 42 days ago

https://github.com/icastillejogomez/degiro-api

An unofficial TypeScript backend client for the DeGiro trading platform. Version 1.0.6 provides functionality for automated order management, fund tracking, product searches, and portfolio monitoring. It supports One-Time Passwords (OTP), session reuse via JSESSIONID, and includes a separate CLI tool for terminal-based interaction.

Tokens
10K
Snippets
31
Records
44
Agent score
53%

What's inside degiro-api

  1. Get JSESSIONID and reuse sessions

    master

    To avoid frequent login/logout requests (which can be flagged by DeGiro), you can extract the jsessionId from an active session and reuse it in a new instance.

    1. Extracting the ID: After calling await degiro.login(), use degiro.getJSESSIONID() (note: this is a synchronous method, not a promise).
    2. Reusing the ID: Pass the jsessionId into the DeGiro constructor along with your credentials. You must then call await degiro.login() to re-hydrate the account configuration data.
    import DeGiro from 'degiro-api'
    
    // 1. Extracting the session ID
    (async () => {
      const degiro = new DeGiro({}) // Uses ENV variables
      await degiro.login()
      const jsessionId = degiro.getJSESSIONID()
    })()
    
    // 2. Reusing the session ID
    (async () => {
      const degiro = new DeGiro({
        username: '<your_username_here>',
        pwd: '<your_password_here>',
        oneTimePassword: '<your_one_time_password_here>',
        jsessionId: '<your_jsessionId_here>',
      })
    
      // Re-hydrate the account config
      await degiro.login()
      // Do your stuff here...
    })()
  2. Create an instance of DeGiro

    master

    To interact with the DeGiro API, you must first create an instance of the DeGiro class. You can provide credentials directly in the constructor, use the static create method, or rely on environment variables.

    Note: This is a backend-only library. It cannot be used in a frontend environment due to DeGiro's CORS policy.

    // Using constructor with credentials
    const DeGiro = require('degiro-api').default
    const degiro = new DeGiro({
      username: '<your_username_here>',
      pwd: '*****'
    })
    
    // OR using the static create method
    const degiro = DeGiro.create({ username: '*****', pwd: '*****' })
    
    // OR using environment variables (DEGIRO_USER & DEGIRO_PWD)
    const degiro = new DeGiro()
  3. Initialize the DeGiro client

    master

    You can initialize the DeGiro client using the constructor or the static create method. The client accepts a DeGiroSettupType object. If credentials are not provided in the object, the client will attempt to read them from the following environment variables:

    • DEGIRO_USER: Your username
    • DEGIRO_PWD: Your password
    • DEGIRO_OTP: Your one-time password (if applicable)
    • DEGIRO_JSESSIONID: An existing session ID to bypass login

    Note: If neither a username nor a jsessionId is provided, the client will throw an error.

    import { DeGiro } from './DeGiro';
    
    // Using the static create method
    const degiro = DeGiro.create({
      username: 'my_username',
      pwd: 'my_password',
      oneTimePassword: '123456'
    });
    
    // Or using environment variables implicitly
    // process.env.DEGIRO_USER = '...';
    // const degiro = new DeGiro();
  4. Retrieve Account Data and Configuration

    master

    Access various aspects of your DeGiro account using the following methods:

    • getAccountConfig(): Returns AccountConfigType.
    • getAccountData(): Returns AccountDataType (note: login() also returns this).
    • getAccountState(options: GetAccountStateOptionsType): Returns an array of account state data.
    • getAccountReports(): Returns AccountReportsType.
    • getAccountInfo(): Returns AccountInfoType.
    import DeGiro from 'degiro-api'
    
    const degiro = new DeGiro({ username: '...', pwd: '...' })
    await degiro.login()
    
    const config = await degiro.getAccountConfig()
    const data = await degiro.getAccountData()
    const state = await degiro.getAccountState()
    const reports = await degiro.getAccountReports()
    const info = await degiro.getAccountInfo()
  5. Search for Products

    master

    Search for financial products using searchProduct(options: SearchProductOptionsType).

    Options:

    • text (required): The search string (e.g., 'AAPL').
    • type (optional): A DeGiroProducTypes enum value.
    • limit (optional): Number of results (default: 10).
    • offset (optional): Pagination offset (default: 0).

    DeGiroProducTypes:

    • shares: 1
    • bonds: 2
    • futures: 7
    • options: 8
    • investmendFunds: 13
    • leveragedProducts: 14
    • etfs: 131
    • cfds: 535
    • warrants: 536
    import DeGiro, { DeGiroEnums } from 'degiro-api'
    const { DeGiroProducTypes } = DeGiroEnums
    
    const degiro = new DeGiro({ username: '...', pwd: '...' })
    await degiro.login()
    
    // Search for TSLA shares with a limit of 1
    const result = await degiro.searchProduct({
      text: 'TSLA',
      type: DeGiroProducTypes.shares,
      limit: 1,
    })
  6. Manage Portfolio Positions

    master

    Fetch your portfolio using getPortfolio(config: GetPorfolioConfigType).

    Config Options:

    • type: Filter positions using PORTFOLIO_POSITIONS_TYPE_ENUM:
      • ALL: All positions.
      • ALL_POSITIONS: Only product positions (excludes 'CASH').
      • OPEN: Only opened positions.
      • CLOSED: Only closed positions.
    • getProductDetails (boolean): If true, results include a productData field with full product details.
    import DeGiro, { DeGiroEnums } from 'degiro-api'
    const { PORTFOLIO_POSITIONS_TYPE_ENUM } = DeGiroEnums
    
    const degiro = new DeGiro({ username: '...', pwd: '...' })
    await degiro.login()
    
    // Get all open positions with full product details
    const portfolio = await degiro.getPortfolio({
      type: PORTFOLIO_POSITIONS_TYPE_ENUM.ALL,
      getProductDetails: true,
    })
  7. Manage DeGiro Sessions

    master

    Use the session endpoints to handle authentication and session state. You must instantiate DeGiro with your credentials before calling login().

    • login(): Authenticates and returns AccountDataType.
    • logout(): Ends the current session.
    • isLogin(options?: IsLoginOptionsType): Checks if the user is currently authenticated. Can be used with { secure: true }.
    • getJSESSIONID(): Returns the current JSESSIONID string if logged in, otherwise undefined.
    import DeGiro from 'degiro-api'
    
    const degiro = new DeGiro({
      username: 'your_username',
      pwd: 'your_password',
    })
    
    // Login
    const accountData = await degiro.login()
    
    // Check status
    const isLogin = await degiro.isLogin({ secure: true })
    
    // Get session ID
    const sessionId = degiro.getJSESSIONID()
    
    // Logout
    await degiro.logout()
  8. Create and Execute Orders

    master

    The API allows for a two-step order process: creating an order and then executing it.

    1. Create an Order

    Use createOrder(order: OrderType).

    OrderType properties:

    • buySell: DeGiroActions (BUY or SELL).
    • orderType: DeGiroMarketOrderTypes (LIMITED: 0, MARKET: 2, STOP_LOSS: 3, STOP_LOSS_LIMIT: 1).
    • price (optional): Limit price.
    • productId: The product ID string.
    • size: Number of units.
    • stopPrice (optional): Stop price.
    • timeType: DeGiroTimeTypes (DAY: 1, PERMANENT: 3).

    2. Execute an Order

    Use executeOrder(order: OrderType, executeId: string) where executeId is the confirmationId returned from createOrder.

    3. Delete an Order

    Use deleteOrder(orderId: string) to remove an order. Note: It is recommended to wait a few seconds after execution before attempting to delete to avoid rate limit errors.

    import DeGiro, { DeGiroEnums, DeGiroTypes } from 'degiro-api'
    const { DeGiroActions, DeGiroMarketOrderTypes, DeGiroTimeTypes } = DeGiroEnums
    const { OrderType } = DeGiroTypes
    
    const degiro = new DeGiro({ username: '...', pwd: '...' })
    await degiro.login()
    
    const order: OrderType = {
      buySell: DeGiroActions.BUY,
      orderType: DeGiroMarketOrderTypes.LIMITED,
      productId: '331868',
      size: 1,
      timeType: DeGiroTimeTypes.DAY,
      price: 272,
    }
    
    // Step 1: Create
    const { confirmationId } = await degiro.createOrder(order)
    
    // Step 2: Execute
    const orderId = await degiro.executeOrder(order, confirmationId)
    
    // Step 3: Delete (after a delay to avoid rate limits)
    setTimeout(async () => {
      await degiro.deleteOrder(orderId)
    }, 2000)
  9. Check if authenticated with isLogin()

    master

    The isLogin() method checks if the instance has account configuration data.

    • Standard check: degiro.isLogin() returns a boolean immediately based on local state. This does not verify if the session is still active on DeGiro's servers.
    • Secure check: await degiro.isLogin({ secure: true }) returns a Promise. It performs a real API request (typically getAccountConfig) to verify that the session is still valid. If it returns false, you should call await degiro.login().
    import DeGiro from 'degiro-api'
    
    (async () => {
      const degiro = new DeGiro({}) // Uses ENV variables
    
      // Force a network request to verify the session is actually alive
      if(! await degiro.isLogin({ secure: true })) {
        await degiro.login()
      }
    })()
  10. Reference: DeGiro Enums and Types

    master

    The following enums and types are available via the DeGiroEnums and DeGiroTypes exports to ensure type safety when constructing orders and searches.

    DeGiroActions

    • BUY: 'BUY'
    • SELL: 'SELL'

    DeGiroMarketOrderTypes

    • LIMITED: 0
    • MARKET: 2
    • STOP_LOSS: 3
    • STOP_LOSS_LIMIT: 1

    DeGiroTimeTypes

    • DAY: 1
    • PERMANENT: 3

    DeGiroProducTypes

    • shares: 1
    • bonds: 2
    • futures: 7
    • options: 8
    • investmendFunds: 13
    • leveragedProducts: 14
    • etfs: 131
    • cfds: 535
    • warrants: 536