Python problem newbie

Hello here! i’m new here :slight_smile:

I’m trying to create a client or an invoice with python helped by chatgpt (I’m a super noob)

I try this code but is not working, I have start the pro version and get the personal token but I got the error after I run my script in the terminal

{“message”:“Invalid Token”,“errors”:{}}

import requests

# Definisci il token e l'URL dell'API di Invoice Ninja
TOKEN = "*********************"  # Assicurati di utilizzare il token fornito
API_URL = "https://invoicing.co/api/v1/clients"  # Sostituisci "example" con il tuo URL di Invoice Ninja

# Dati del cliente
client_data = {
    "name": "Mario Rossi"
}

# Impostazioni dell'intestazione con il token
headers = {
    "X-Ninja-Token": TOKEN,
    "Content-Type": "application/json",
}

# Effettua la richiesta POST per creare il cliente
response = requests.post(API_URL, json=client_data, headers=headers)

# Controlla lo stato della risposta
if response.status_code == 200:
    print("Cliente creato con successo!")
    print(response.json())  # Stampa la risposta JSON (opzionale)
else:
    print("Si è verificato un errore durante la creazione del cliente:")
    print(response.text)  # Stampa il testo dell'errore

Who can help me? I can’t find any example file for create something in python, thank you

Hi,

The header key should be X-API-Token not X-Ninja-Token.

Hi @hillel thank you, but now I got this error in terminal

{“code”:57,“message”:“You are not authorized to perform this operation”}
vittoriogrossi@MacBook-Air-3 SCRIPT % python3 Invoice.py
Traceback (most recent call last):
File “/Users/vittoriogrossi/Downloads/BotEtoro/SCRIPT/Invoice.py”, line 17, in
response = requests.post(API_URL, json=client_data, headers=headers)
^^^^^^^^
NameError: name ‘requests’ is not defined

There are not too much documens about python integration on invoicing

Not sure, that looks like a Python error