Skip to main content

pyfortnox

Fortnox API V3 library client for Python

Installation

pyfortnox package can be installed either via pip or easy_install:

$ pip install --upgrade pyfortnox

or

$ easy_install --upgrade pyfortnox

You can install from the source code as well. First clone the repo and then execute:

$ python setup.py install

After installing, import pyfortnox package:

import fortnox

Usage

import fortnox

Build a client

Using this api without authentication gives an error

# Then we instantiate a client (as shown below)
client = fortnox.Client(
    access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)

Client Options

The following options are available while instantiating a client:

  • access_token: Personal access token
  • client_secret: Private/public integration app's client secret
  • base_url: Base url for the api
  • timeout: Request timeout
  • verbose: Verbose/debug mode

Architecture

The library follows few architectural principles you should understand before digging deeper.

  1. Interactions with resources are done via service objects.
  2. Service objects are exposed as properties on client instances.
  3. Service objects expose resource-oriented actions.
  4. Actions return dictionaries that support attribute-style access, a la JavaScript (thanks to Bunch and it's form Munch).

For example, to interact with projects API you will use fortnox.ProjectService, which you can get if you call:

client = fortnox.Client(
    access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)
client.projects # fortnox.ProjectService

To retrieve list of resources and use filtering you will call list() method:

client = fortnox.Client(
    access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)
client.projects.list(organization_id=google.id, hot=True) # list(dict|Munch)

or simply client.projects.list()

To find custom field by name and its value pass kwargs as an argument:

client = fortnox.Client(
    access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)
client.projects.list(**{'ProjectNumber': 1})

To find a resource by its unique identifier use retrieve() method:

client = fortnox.Client(
    access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)
client.projects.retrieve(id=1)

When you'd like to create a resource, or update it's attributes you want to use either create() or update() methods. For example if you want to create a new project you will call:

client = fortnox.Client(
    access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)
project = client.projects.create(Description='Website design', Status='ONGOING')

project.ProjectNumber = 1
project.Status = 'ONGOING'
project.Description = 'Website redesign'

client.projects.update(project.ProjectNumber, StartDate='2014-02-28')

To destroy a resource use destroy() method:

client = fortnox.Client(
    access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)
client.projects.destroy(project.ProjectNumber)

There other non-CRUD operations supported as well. Please contact corresponding service files for in-depth documentation.

Full example

# Obtain an access token using a new authorization code. This access token will
# then be used for any further API calls.
client = fortnox.Client(
    authorization_code='<YOUR_APP_INTEGRATION_AUTHORIZATION_CODE>',
    client_secret='<YOUR_APPS_CLIENT_SECRET>'
)
obtained_token = client.token.access_token()
access_token = obtained_token.AccessToken

Handling Exceptions

When you instantiate a client or make any request via service objects, exceptions can be raised for multiple of reasons e.g. a network error, an authentication error, an invalid param error etc.

Sample below shows how to properly handle exceptions:

try:
    # Instantiate a client.
    client = fortnox.Client(
        access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
        client_secret='<YOUR_APPS_CLIENT_SECRET>'
    )
    project = client.projects.create(Description='Website design', Status='ONGOING')
    print(project)
except fortnox.ConfigurationError as e:
    #  Invalid client configuration option
    pass
except fortnox.ResourceError as e:
    # Resource related error
    print('Http status = ' + e.http_status)
    print('Request ID = ' + e.logref)
    for error in e.errors:
        print('field = ' + error.field)
        print('code = ' + error.code)
        print('message = ' + error.message)
        print('details = ' + error.details)
except fortnox.RequestError as e:
    # Invalid query parameters, authentication error etc.
    pass
except Exception as e:
    # Other kind of exceptioni, probably connectivity related
    pass

Sample below shows how to send files via inbox service with handled exceptions:

try:
    # Instantiate a client.
    client = fortnox.Client(
        access_token='<YOUR_PERSONAL_ACCESS_TOKEN>',
        client_secret='<YOUR_APPS_CLIENT_SECRET>'
    )

    from io import BytesIO
    file =  open('/your/local/file/path/voucher_file.jpeg', 'rb')

    buffered_file = BytesIO(file.read())
    file_name = 'voucher1.jpg'

    voucher_file = client.inbox.create(path='inbox_v', file=buffered_file, file_name=file_name)

    print(voucher_file)
except fortnox.ConfigurationError as e:
    #  Invalid client configuration option
    pass
except fortnox.ResourceError as e:
    # Resource related error
    print('Http status = ' + e.http_status)
    print('Request ID = ' + e.logref)
    for error in e.errors:
        print('field = ' + error.field)
        print('code = ' + error.code)
        print('message = ' + error.message)
        print('details = ' + error.details)
except fortnox.RequestError as e:
    # Invalid query parameters, authentication error etc.
    pass
except Exception as e:
    # Other kind of exceptioni, probably connectivity related
    pass

Resources and actions

Documentation for every action can be found under fortnox/services/ files.

To know about available services, see Fortnox's Official Developer Documentation https://developer.fortnox.se/documentation/

N.B. Below services are not implemented in the latest release of pyfortnox:

  1. Digital Receipt
  2. Warehouse Custom Inbound Documents
  3. Warehouse Custom Outbound Documents
  4. Warehouse Information
  5. Warehouse Item Summary
  6. Warehouse Resource Specific Fields

Tests

To run all test suites:

$ python setup.py test

And to run a single suite:

$ python setup.py test -s fortnox.test.test_associated_project_service.ProjectServiceTests

Thanks

I would like to give huge thanks to my wife, fellow colleagues, mentors and friends for their continuous inspiration and supports to contribute to this package. pyfortnox was named from pythonic fortnox and I was lucky to publish wrapper under pyfortnox name.

Thank You!

License

MIT

Bug Reports

Report here

Contact

Mahmudul Hasan (ikhtiarcse10ruet@gmail.com)

Powered By

JetBrains Logo

Metadata

Release files for pyfortnox 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfortnox 1.2.0
File Size Uploaded Provenance Actions
pyfortnox-1.2.0.tar.gz 35.6 kB Without provenance Details for pyfortnox-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyfortnox 1.2.0
File Interpreter ABI Platform Actions
pyfortnox-1.2.0-py3-none-any.whl Python 3 none any Details for pyfortnox-1.2.0-py3-none-any.whl

Total release size: 133.0 kB

Release files / pyfortnox-1.2.0.tar.gz

Download URL pyfortnox-1.2.0.tar.gz
Size 35.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0f110ee528d88c98bf794070d5f032b6195fac95e3bed32f8d84e43a1c69c4d1
BLAKE2b-256 checksum
How to use checksums
2636f9e9723d18917ee8d48edb9905119ebf2dbdb03ee85e600a649bb90e6a41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

Release files / pyfortnox-1.2.0-py3-none-any.whl

Download URL pyfortnox-1.2.0-py3-none-any.whl
Size 97.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
883fb4b12bedb65f5a5a5938e5e89693450f6355bb3cc70fcd6fec646037990f
BLAKE2b-256 checksum
How to use checksums
085e3fcd98e1d0f9a71eef679e0f6a16775838316fa2dcec901bf00f78067436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

Security for pyfortnox 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page