Skip to content

Basic example (python)

# -*- coding:utf-8 -*-
#
#
#  IMX webservice basic call example
#

import base64
import requests

URL = "https://webservice.imxpostal.fr/smc/rpc/"
# Credentials data given by IMX.
USERNAME = "..." # given by IMX
PASSWORD = "..."  # given by IMX
ACCOUNT = "..." # given by IMX

data = {
    "jsonrpc": "2.0",
    "method": "api.declare_parcel",
    # Request identifier. At the customer's convenience, not necessarily unique.
    "id": "__test__",
    "params": [
        USERNAME,
        PASSWORD,
        {
            "account": ACCOUNT,
            "addressee": {
                "name": "Jean Dupond",
                "company": "Dupond & Dupont",
                "address1": "24 rue de l\'Est",
                "adresse2": "Bâtiment 2",
                "postcode":"60240",
                "town":"Chaumont-en-Vexin",
                "country":"FR",
                "phone":"0615359402",
                "email":"test@gmail.com",
            },
            "sender": {
                "company": "Ma petite entreprise",
                "address1": "72 Rue Elie Cartan",
                "postcode": "75012",
                "town": "Paris",
                "country": "FR",
            },
            "parcel_description" : {
                # Weight in kilograms
                "weight": 0.3,
            },
            "options":
                {
                    "label": "True",
                    "label_format": "pdf",
                    "service": "1",

                },
            "reference": "TEST_DOC_PYTHON",
            "version": 1,
        },
    ],
}

xx = requests.post(URL, json=data)
try:
    output = xx.json()
    print(output)
    result = output["result"]
except:
    output = xx.text
    print(output)
else:
    pdf_content = base64.b64decode(result["label"])
    pdf_filename = "test-basic.pdf"
    open(pdf_filename, "wb").write(pdf_content)

# The answer to this query is of the following type:
# {
#     "success": True,
#     "errors": []
#     "warnings": []
#     "label": <binary data>
#     "label_source": "gls"
#     "tracking_data": {
#             "imx_tracking_id" : "1654125",
#             "imx_tracking_url" : "http://www.imxpostal.fr/suivi/colis/suivi/1654125/html/",
#             "carrier_tracking_id" : "008DXL3X",
#             "carrier_tracking_url" : "http://www.gls-group.eu/276-I-PORTAL-WEB/content/GLS/FR01/FR/5004.htm?txtAction=71000&txtRefNo=008DXL3X",
#         "carrier_tracking_website": "http://www.gls-group.eu/276-I-PORTAL-WEB/content/GLS/FR01/FR/5004_-suivi-de-colis.htm"
#         }
#     }