Create Invoice

 1# This is an example where we create a new client and an invoice for them.
 2
 3from datetime import date
 4from freshbooks import Client as FreshBooksClient
 5from freshbooks import FreshBooksError
 6
 7fb_client_id = "<your client id>"
 8access_token = "<your access token>"
 9account_id = "<your account id>"
10
11freshBooksClient = FreshBooksClient(client_id=fb_client_id, access_token=access_token)
12
13# Create the client
14print("Creating client...")
15try:
16    client_data = {"organization": "Python SDK Test Client"}
17    client = freshBooksClient.clients.create(account_id, client_data)
18except FreshBooksError as e:
19    print(e)
20    print(e.status_code)
21    exit(1)
22
23print(f"Created client {client.id}")
24
25# Create the invoice
26line1 = {
27    "name": "Fancy Dishes",
28    "description": "They're pretty swanky",
29    "qty": 6,
30    "unit_cost": {
31        "amount": "27.00",
32        "code": "CAD"
33    }
34}
35line2 = {
36    "name": "Regular Glasses",
37    "description": 'They look "just ok"',
38    "qty": 8,
39    "unit_cost": {
40        "amount": "5.95",
41        "code": "CAD"
42    }
43}
44invoice_data = {
45    "customerid": client.id,
46    "create_date": date.today().isoformat(),
47    "lines": [line1, line2],
48}
49print("Creating invoice...")
50try:
51    invoice = freshBooksClient.invoices.create(account_id, invoice_data)
52except FreshBooksError as e:
53    print(e)
54    print(e.status_code)
55    exit(1)
56
57print(f"Created invoice {invoice.id}")
58print(f"Invoice total is {invoice.amount.amount} {invoice.amount.code}")
59
60# Invoices are created in draft status, so we need to mark it as sent
61print("Marking invoice as sent...")
62invoice_data = {
63    "action_mark_as_sent": True
64}
65invoice = freshBooksClient.invoices.update(account_id, invoice.id, invoice_data)