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    "due_offset_days": 5,  # due 5 days after create_date
48    "lines": [line1, line2],
49}
50print("Creating invoice...")
51try:
52    invoice = freshBooksClient.invoices.create(ACCOUNT_ID, invoice_data)
53except FreshBooksError as e:
54    print(e)
55    print(e.status_code)
56    exit(1)
57
58print(f"Created invoice {invoice.invoice_number} (Id: {invoice.id})")
59print(f"Invoice total is {invoice.amount.amount} {invoice.amount.code}")
60
61# Invoices are created in draft status, so we need to mark it as sent
62print("Marking invoice as sent...")
63invoice_data = {
64    "action_mark_as_sent": True
65}
66try:
67    invoice = freshBooksClient.invoices.update(ACCOUNT_ID, invoice.id, invoice_data)
68except FreshBooksError as e:
69    print(e)
70    print(e.status_code)
71    exit(1)