Account Information API

The nbg.account_information module provides a Python interface to the NBG Account Information API, which is a PSD2 compliant account information interface exposing details of the requested accounts, balances and transactions.

More information about the NBG Account Information API can be found at https://developer.nbg.gr/apiProduct/Account-Information-PSD2.

Getting started

Before you get started you need to:

  1. Create an NBG developer account at https://developer.nbg.gr/

  2. Create an organization at https://developer.nbg.gr/organizations

  3. Create an app for your organization

  4. Subscribe your application to the Account Information - PSD2 API

  5. Note down your Client ID and Client Secret

Authentication

The Account Information API utilizes OAuth2 for authentication. The OAuth2 authentication flow can be described in a few steps:

  1. Prompt user to visit the URL from get_authorization_code_url().

  2. After they authenticate with their NBG account, they will be redirected to the redirect_url you provided

  3. Grab the authorization_code from the code GET parameters in the redirect_url

  4. Exchange the authorization_code for an access_token with set_access_token_from_authorization_code().

from nbg.account_information import AccountInformationPSD2Client

client = AccountInformationPSD2Client(
    client_id="your_app_client_id",
    client_secret="your_app_client_secret",
)

# Let's assume that this code runs in a Django view, where the
# `request` object is available.
authorization_code = request.GET["code"]
client.set_access_token_from_authorization_code(authorization_code)

accounts = client.accounts(user_id="your_user_id")

Authentication API reference

AccountInformationPSD2Client.get_authorization_code_url(redirect_uri: str, scope: str = None, response_type: str = 'code') → str

Composes and returns the URL that has to be visited by a user to get an authorization code for the current client.

Parameters
  • redirect_uri (string) – The redirect URI to return the authorization code as GET parameter.

  • scope (string) – The OAuth scope for which to get authorization code. Defaults to None; this is each client’s built-in configuration, which should suffice in most cases.

  • response_type (string) – The response type when exchanging the authorization code. Defaults to token, which should suffice in most cases.

Usage

client.get_authorization_code_url(
    redirect_uri="https://myapp.example.com/complete/nbg/",
)
AccountInformationPSD2Client.set_access_token_from_authorization_code(authorization_code: str, redirect_uri: str)

Exchanges an authorization code with an access token and sets the access token accordingly for the current client.

Parameters
  • authorization_code (string) – The authorization code you received as a GET parameter.

  • redirect_uri (string) – The redirect URI for which you requested the authorization code.

Usage

client.set_access_token_from_authorization_code(
    authorization_code="the_authorization_code_you_received",
    redirect_uri="https://myapp.example.com/complete/nbg/",
)
AccountInformationPSD2Client.set_access_token(access_token: str)

Sets the access token for the current client.

Parameters

access_token (string) – The access token to set up for the current client.

Usage

client.set_access_token("the_access_token_of_a_user")

Consents

The Account Information API utilizes the concept of concerns to ensure that API actions are authorized by the user at any given time. You can utilize the consent flow, after completing successfully the authentication flow described above. The consent flow can be described in a few steps:

  1. Generate a consent via generate_consent().

  2. Store the consent ID for the current client session via set_consent_id().

  3. Prompt user to provide their consent by visiting the URL from get_user_consent_url().

from nbg.account_information import AccountInformationPSD2Client

client = AccountInformationPSD2Client(
    client_id="your_app_client_id",
    client_secret="your_app_client_secret",
)

# Complete authentication flow successfully first.

consent = client.generate_consent()
client.set_consent_id(consent["consentId"])

# Navigate the user to the following URL to get their consent
consent_url = client.get_user_consent_url(
    redirect_url="https://myapp.example.com/nbg/consent/"
)

Consents API reference

Generate a consent ID for use by the current client.

Usage

client.generate_consent()

Set the consent ID for the current client.

Parameters

consent_id (string) – The consent ID from generate_consent()

Usage

client.set_consent_id("my-unique-consent-id")

Get URL to present to the user to provide their consent.

Parameters

redirect_url (string) – The URL to redirect to, after the consent is given

Usage

client.get_user_consent_url(
  redirect_url="https://myapp.example.com/nbg/consent/"
)

Returns information for the requested consent.

Parameters
  • consent_id (string) – The consent ID to get information

  • user_id (string) – The user ID of the user that provided the consent

Usage

client.get_consent_information(
  consent_id="your-unique-consent-id",
  user_id="your_user_id",
)

Delete the requested consent.

Parameters
  • consent_id (string) – The consent ID to delete

  • user_id (string) – The user ID of the user that provided the consent

  • tan_number (string) – String "smsotp" to receive TAN number on your mobile phone, or the TAN number you received.

Usage

client.delete_consent(
  consent_id="your-unique-consent-id",
  user_id="your_user_id",
  tan_number="smsotp",  # Set to "smsotp" to send code you user's mobile phone
)

# After you receive the ``tan_number``.
tan_number = "the_one_the_user_received"
client.delete_consent(
  consent_id="your-unique-consent-id",
  user_id="your_user_id",
  tan_number=tan_number,  # Set to the code received by the user
)

Sandbox mode

The AccountInformationPSD2Client runs in sandbox mode by default, and it should do so, unless it’s running on a live production environment. In a typical sandbox scenario, you would:

  1. Create a unique sandbox, in case you don’t have one created already

  2. Set the sandbox_id for your client

  3. Use AccountInformationPSD2Client

from nbg.account_information import AccountInformationPSD2Client

client = AccountInformationPSD2Client(
    client_id="your_app_client_id",
    client_secret="your_app_client_secret",
)

sandbox_id = "your-unique-sandbox-id"
client.create_sandbox(sandbox_id)
client.set_sandbox(sandbox_id)

accounts = client.accounts(user_id="your_user_id")

Sandbox API reference

AccountInformationPSD2Client.create_sandbox(sandbox_id: str) → dict

Create a sandbox with the given sandbox_id.

Parameters

sandbox_id (string) – The unique ID of the sandbox to be created.

Usage

client.create_sandbox("my-unique-sandbox-id")
AccountInformationPSD2Client.export_sandbox(sandbox_id: str) → dict

Returns all contents of the sandbox identified by the given sandbox_id.

Parameters

sandbox_id (string) – The unique ID of the sandbox to get its data.

Usage

client.export_sandbox("my-unique-sandbox-id")
AccountInformationPSD2Client.import_sandbox(sandbox_id: str, data: dict) → dict

Imports the given data into the sandbox identified by the given sandbox_id.

Parameters
  • sandbox_id (string) – The unique ID of the sandbox into which to import data.

  • data (dict) – The JSON data to import into the sandbox

Usage

sandbox_data = {"sandbox": "data", "key": "value"}
client.import_sandbox("my-unique-sandbox-id", sandbox_data)
AccountInformationPSD2Client.delete_sandbox(sandbox_id: str) → bool

Deletes the sandbox identified by the given sandbox_id.

Parameters

sandbox_id (string) – The unique ID of the sandbox to delete.

Usage

client.delete_sandbox("my-unique-sandbox-id")
AccountInformationPSD2Client.set_sandbox(sandbox_id: str)

Sets the sandbox ID to be used by the current client.

Parameters

sandbox_id (string) – The unique ID of the sandbox to use in subsequent API requests.

Usage

client.set_sandbox("my-unique-sandbox-id")

AccountInformationPSD2Client

class nbg.account_information.AccountInformationPSD2Client(client_id: str, client_secret: str, production: bool = False)

The AccountInformationPSD2Client is the Python interface for NBG’s Account Information API.

Parameters
  • client_id (string) – The Client ID you received for your NBG application.

  • client_secret (string) – The Client Secret you received for your NBG application.

  • production (bool) – Whether the client should run in production mode (True) or sandbox mode (False). Defaults to False.

Usage

from nbg.account_information import AccountInformationPSD2Client
client = AccountInformationPSD2Client(
    client_id="your_app_client_id",
    client_secret="your_app_client_secret",
)
account_beneficiaries(user_id: str, iban: str) → dict

List beneficiaries of a domestic account.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • iban (string) – The IBAN of the domestic account.

Usage

client.account_beneficiaries(
    user_id="your_user_id",
    iban="GR7701100800000008000123456",
)
account_details(user_id: str, account: str) → dict

Retrieve details of a domestic account.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • account (string) – The number of the domestic account.

Usage

client.account_details(
    user_id="your_user_id",
    account="8000123456",
)
account_transactions(user_id: str, account: str, date_from: datetime.datetime, date_to: datetime.datetime) → dict

List transactions of a domestic account in a given time period.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • account (string) – The number of the domestic account.

  • date_from (datetime) – The datetime after which to look for transactions.

  • date_to (datetime) – The datetime after which to look for transactions.

Usage

from datetime import datetime
client.account_transactions(
    user_id="your_user_id",
    account="8000123456",
    date_from=datetime(2020, 1, 1),
    date_to=datetime(2020, 12, 31),
)
accounts(user_id: str) → dict

List domestic accounts in Εuro for the given user.

Parameters

user_id (string) – The user ID of the corresponding user.

Usage

client.accounts(user_id="your_user_id")
card_details(user_id: str, card_number: str) → dict

Retrieve detailed information for the given credit or debit card.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • card_number (string) – The number of the card.

Usage

client.card_details(
    user_id="your_user_id",
    card_number="4111111111111111",
)
card_transactions(user_id: str, card_number: str, date_from: datetime.datetime, date_to: datetime.datetime) → dict

List transactions of a credit or debit cart in a given time period.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • card_number (string) – The number of the card.

  • date_from (datetime) – The datetime after which to look for transactions.

  • date_to (datetime) – The datetime after which to look for transactions.

Usage

client.card_transactions(
    user_id="your_user_id",
    card_number="4111111111111111",
    date_from=datetime(2020, 1, 1),
    date_to=datetime(2020, 12, 31),
)
cards(user_id: str) → dict

List of credit and debit cards for the given user.

Parameters

user_id (string) – The user ID of the corresponding user.

Usage

client.cards(
    user_id="your_user_id",
)
foreign_currency_account_beneficiaries(user_id: str, account: str) → dict

List beneficiaries of a foreign currency account.

Parameters
  • user_id (string) – The user ID of the corresponding user.

  • account (string) – The number of the foreign currency account.

Usage

client.foreign_currency_account_beneficiaries(
    user_id="your_user_id",
    account="8000123456",
)
foreign_currency_account_details(user_id: str, account: str) → dict

Retrieve details of a foreign currency account.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • account (string) – The number of the foreign currency account.

Usage

client.foreign_currency_account_details(
    user_id="your_user_id",
    account="8000123456",
)
foreign_currency_account_transactions(user_id: str, account: str, date_from: datetime.datetime, date_to: datetime.datetime) → dict

List transactions of a foreign currency account in a given time period.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • account (string) – The number of the foreign currency account.

  • date_from (datetime) – The datetime after which to look for transactions.

  • date_to (datetime) – The datetime after which to look for transactions.

Usage

from datetime import datetime
client.foreign_currency_account_details(
    user_id="your_user_id",
    account="8000123456",
    date_from=datetime(2020, 1, 1),
    date_to=datetime(2020, 12, 31),
)
foreign_currency_accounts(user_id: str) → dict

List accounts in foreign currencies (e.g. USD) for the given user.

Parameters

user_id (string) – The user ID of the corresponding user.

Usage

client.foreign_currency_accounts(user_id="your_user_id")
scheduled_payments(user_id: str, account: str, date_from: datetime.datetime, date_to: datetime.datetime) → dict

List scheduled payments of a domestic account in a given time period.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • account (string) – The number of the domestic account.

  • date_from (datetime) – The datetime after which to look for scheduled payments.

  • date_to (datetime) – The datetime until which to look for scheduled payments.

Usage

from datetime import datetime
client.scheduled_payments(
    user_id="your_user_id",
    account="8000123456",
    date_from=datetime(2020, 1, 1),
    date_to=datetime(2020, 12, 31),
)
standing_orders(user_id: str, account: str, date_from: datetime.datetime, date_to: datetime.datetime) → dict

List standing orders of a domestic account in a given time period.

Parameters
  • user_id (string) – The user ID of user owning the account.

  • account (string) – The number of the domestic account.

  • date_from (datetime) – The datetime after which to look for standing orders.

  • date_to (datetime) – The datetime until which to look for standing orders.

Usage

from datetime import datetime
client.standing_orders(
    user_id="your_user_id",
    account="8000123456",
    date_from=datetime(2020, 1, 1),
    date_to=datetime(2020, 12, 31),
)