Carrier Integration
Karrio is designed to be easily extensible with new carrier integrations. This guide will walk you through the process of creating a custom carrier integration, from setting up the initial structure to implementing the specific API calls and data mapping.
Overview
Integrating a new carrier into Karrio involves several key steps:
- Bootstrapping the Extension: Creating the initial package structure and boilerplate code
- Generating Schemas: Converting carrier API documentation into Python types
- Defining Metadata: Setting up carrier connection settings and data units
- Implementing API Requests: Creating the communication layer with the carrierβs API
- Mapping Data: Transforming between Karrioβs unified format and carrier-specific formats
- Testing and Validation: Ensuring the integration works correctly
Prerequisites
Before you begin, make sure you have:
- A development environment set up for Karrio
- Basic knowledge of Python programming
- Access to the carrierβs API documentation
- Understanding of the carrierβs API request/response format
Extension Structure Overview
A Karrio carrier extension follows a standardized structure. Hereβs the recommended structure:
1modules/connectors/[carrier_name]/ 2βββ setup.py # Package setup file 3βββ generate # Schema generation script 4βββ schemas/ # API schema files 5β βββ rate_request.json # (or .xsd for XML APIs) 6β βββ rate_response.json 7β βββ ... 8βββ karrio/ 9 βββ plugins/ # Plugin registration (new structure) 10 β βββ [carrier_name]/ 11 β βββ __init__.py # Contains METADATA 12 βββ mappers/ # Integration layer 13 β βββ [carrier_name]/ 14 β βββ __init__.py # Contains METADATA (legacy structure) 15 β βββ mapper.py # Data mapping functions 16 β βββ proxy.py # API client 17 β βββ settings.py # Connection settings 18 βββ providers/ # Provider-specific code 19 β βββ [carrier_name]/ 20 β βββ __init__.py 21 β βββ units.py # Enums & constants 22 β βββ utils.py # Utility functions 23 β βββ error.py # Error handling 24 β βββ rate.py # Rating implementation 25 β βββ tracking.py # Tracking implementation 26 β βββ shipment/ # Shipping implementation 27 β β βββ __init__.py 28 β β βββ create.py 29 β β βββ cancel.py 30 β βββ ... 31 βββ schemas/ # Generated API data types 32 βββ [carrier_name]/ 33 βββ __init__.py 34 βββ rate_request.py 35 βββ rate_response.py 36 βββ ...
Extension Anatomy
At the core of Karrioβs design is a modular architecture that separates carrier integrations from the unified interface abstraction. Each carrier integration is contained in its own Python package, making it easy to add, update, or remove carriers without affecting the rest of the system.
Key Components
-
Metadata: Defined in the pluginβs
__init__.py
file (either inplugins/[carrier_name]/__init__.py
ormappers/[carrier_name]/__init__.py
), this identifies your extension to Karrio. -
Settings: The
settings.py
file defines the connection parameters needed for the carrierβs API. -
Proxy: The
proxy.py
file implements the API client that communicates with the carrierβs services. -
Mapper: The
mapper.py
file contains functions that transform data between Karrioβs unified format and carrier-specific formats. -
Schemas: Generated Python data types that represent the carrierβs API request and response structures.
Bootstrapping a New Extension
Karrio provides a CLI tool to quickly scaffold a new carrier extension. The tool creates all the necessary files and directories with the appropriate structure.
Run the extension creation command1kcli add-extension
This will start an interactive process:
1Carrier slug: freight_express # Unique carrier identifier (e.g., dhl_express, ups, fedex) 2Display name: Freight Express # Human-readable carrier name (e.g., DHL, UPS, FedEx) 3Features [tracking, rating, shipping]: # The features you want to implement 4Version [2024.3]: # The extension version 5Is XML API? [y/N]: # Whether the carrier uses XML for API communication
After confirmation, the tool will generate the extension structure in the modules/connectors/[carrier_name]
directory.
Detailed Guides
To complete your carrier integration, follow these detailed guides:
- Schema Generation: Learn how to generate Python data types from carrier API documentation
- Metadata: Define carrier connection settings and data units
- API Requests: Implement API calls for different carrier operations
- Data Mapping: Transform data between Karrioβs unified format and carrier-specific formats
Final Steps
After implementing your carrier integration, youβll need to:
- Test your integration thoroughly with unit tests
- Set up the carrier in the Karrio dashboard
Example Integrations
For reference, you can explore existing carrier integrations in the Karrio codebase:
These integrations cover a variety of API types (REST, SOAP) and carrier features (rating, shipping, tracking, etc.).
Troubleshooting
If your carrier integration isnβt working correctly:
- Check the extension structure matches the expected format
- Ensure the METADATA object is properly defined in the
__init__.py
file - Verify all required dependencies are installed
- Look for import errors in the application logs
- Use
get_failed_plugin_modules()
to get information about plugins that failed to load
For more information on plugin loading and registration, see the Plugin Development Guide.