Local Development
Welcome to the Karrio development guide! This comprehensive resource will help you set up your local environment to contribute to Karrio or build custom integrations. Whether you’re adding a new carrier integration, enhancing the API, or improving the dashboard, this guide has you covered.
Architecture Overview
Karrio is built with a powerful and flexible microservices architecture, consisting of several components:
- API Server: Django REST Framework application that provides the core API
- Dashboard: React application for the user interface
- Worker: Huey workers for background tasks
- Database: PostgreSQL for data storage (SQLite supported for development)
- Cache: Redis for caching and as a message broker
Prerequisites
Before you begin, make sure you have the following installed:
- Git
- Node.js v16.15.1+
- Python 3.8+
- Docker & Docker Compose (optional, for Docker-based setup)
Windows users are strongly recommended to use Windows Subsystem for Linux (WSL) for the best development experience. Windows versions before Windows 10 are not officially supported.
Manual Setup (Recommended)
Follow these steps to set up your development environment manually:
1. Clone the Repository
First, fork the Karrio repository on GitHub, then clone your fork:
1git clone https://github.com/[YOUR_USERNAME]/karrio.git 2cd karrio
2. Install System Dependencies
Install the required system dependencies:
1./bin/install-binaries
This script installs system dependencies based on your operating system:
- On macOS:
gcc
,libffi
, andzint
- On Linux:
libpango1.0-0
,libpangoft2-1.0-0
,gcc
, andzint
These libraries are required for PDF generation, font rendering, and barcode generation functionality.
If you encounter permission issues:
1chmod +x ./bin/*
3. Set up Python Environment
Create and configure a Python virtual environment with all dependencies:
1source ./bin/setup-server-env
This command will:
- Create a new Python virtual environment
- Install server development requirements
- Install insiders development requirements (if available)
- Install platform development requirements (if available)
- Install Node.js dependencies
- Set up logging directories
4. Apply Database Migrations
By default, Karrio uses SQLite for development to simplify setup:
1karrio migrate
Using PostgreSQL Instead
If you prefer PostgreSQL, modify your environment configuration:
1- DATABASE_ENGINE=sqlite3 2- # DATABASE_ENGINE=postgresql_psycopg2 3- # DATABASE_USERNAME=postgres 4- # DATABASE_PASSWORD=postgres 5- # DATABASE_NAME=db 6+ # DATABASE_ENGINE=sqlite3 7+ DATABASE_ENGINE=postgresql_psycopg2 8+ DATABASE_USERNAME=postgres 9+ DATABASE_PASSWORD=postgres 10+ DATABASE_NAME=db
Then start the PostgreSQL container:
1docker compose -f docker/docker-compose.yml up -d db
5. Collect Static Assets
Gather static files for the web interface:
1karrio collectstatic --noinput
The collectstatic
command is crucial for both development and production. Without it, CSS and JavaScript will not be served correctly, resulting in a broken UI.
6. Create Admin User
Create your admin account:
1karrio createsuperuser
7. Start the Server
Launch the Karrio server:
1./bin/start
Docker-based Setup (Alternative)
If you prefer using Docker for development, here’s how to set it up:
Quick Start with Docker
Clone the repository1git clone https://github.com/[YOUR_USERNAME]/karrio.git 2cd karrio 3 4# Create and setup the Docker development environment 5./bin/docker-env create 6 7# Start the Karrio server 8./bin/docker-env exec './bin/start-server'
Once running, Karrio will be available at http://localhost:5002
The Docker environment provides a fully configured setup with all dependencies installed. This is often the easiest way to get started, especially if you’re new to the project.
For more Docker commands and options:
1./bin/docker-env
This will display a help menu with all available commands like:
1Help: You can pass any the following commands to the './bin/docker-env' scripts 2----- 3create Create and setup docker dev environment. 4destroy Destroy docker dev environment. 5on Start docker dev environment. 6off Stop docker dev environment. 7shell Start docker dev environment shell. 8exec Execute command in docker dev environment.
Example Docker Commands
Run server tests:
1./bin/docker-env exec './bin/run-server-tests'
Run tests for a specific carrier:
1./bin/docker-env exec 'python -m unittest discover -v -f modules/connectors/dhl_express/tests'
Setting Up the Dashboard
The Karrio dashboard is now part of the monorepo structure. To set it up:
1. Install Node Dependencies
At the root of the repository:
1npm i
2. Configure Environment
Copy the sample environment file:
1cp apps/dashboard/.env.sample apps/dashboard/.env
3. Start the Dashboard
Launch the development server:
1npm run dev
Development Workflows
Karrio provides specialized workflows for different development tasks.
Working on the SDK and Carrier Extensions
Set up the SDK environment:
1cd karrio 2source ./bin/setup-sdk-env
Run all SDK tests:
1./bin/run-sdk-tests
Working on a Single Carrier
Create a Python environment with the necessary dependencies:
1cd karrio 2./bin/create-new-env
Install the extension in development mode:
Install the core SDK1pip install -e modules/sdk 2 3# Install a specific carrier extension 4pip install -e modules/connectors/fedex
Run tests for a specific carrier:
Format:1# python -m unittest discover -v -f modules/connectors/[carrier_extension]/tests 2python -m unittest discover -v -f modules/connectors/fedex/tests
Working on the Server
Set up the server environment:
1cd karrio 2source ./bin/setup-server-env
Run all server tests:
1./bin/run-server-tests
Run tests for a specific module:
Format:1# karrio test --failfast karrio.server.[module].tests 2karrio test --failfast karrio.server.manager.tests
Database Management
Reset Database
To completely reset your database for a fresh installation:
Stop and remove the database container1docker compose -f docker/docker-compose.yml down 2 3# Start a fresh PostgreSQL container 4docker compose -f docker/docker-compose.yml up -d db
Backup Data
Create a backup of your current database:
1karrio dumpdata --natural-foreign --natural-primary -o [BACKUP_PATH]/data[VERSION].json 2# Example: 3# karrio dumpdata --natural-foreign --natural-primary -o ../backup/data2024.1.json
Load Development Data
Restore data from a backup:
1karrio loaddata [BACKUP_PATH]/data[VERSION].json 2# Example: 3# karrio loaddata ../backup/data2024.1.json
Code Organization
The Karrio codebase is organized as follows:
/apps
: Web applications and interfaces/apps/api
: Django REST API server for external integrations/apps/dashboard
: React-based admin dashboard UI/apps/web
: Next.js documentation and marketing website/apps/www
: Legacy website components
/modules
: Core functionality and services/modules/sdk
: Core shipping SDK that powers all integrations/modules/core/
: Core shared functionality/modules/manager/
: Account and organization management/modules/documents/
: Document generation services/modules/events/
: Event processing and webhooks/modules/orders/
: Order management and fulfillment/modules/graph/
: GraphQL API implementation
/bin
: Helper scripts for development, testing, and deployment/docker
: Docker configurations and container definitions/schemas
: API schema definitions and validations/ee
: Enterprise Edition extensions (when applicable)
Adding a New Carrier
One of the most common contributions is adding support for a new shipping carrier:
- Create a new carrier module in
/modules/connectors/
- Implement the required interfaces:
CarrierInterface
: Base interface for all carriersShippingInterface
: For shipping operationsTrackingInterface
: For tracking operationsRateInterface
: For rate calculation
- Add carrier-specific settings
- Add comprehensive tests
- Update documentation
For a detailed guide, see Adding a New Carrier.
Troubleshooting
Common Issues
Permission Denied Errors
If you encounter permission errors when running shell scripts, try making them executable:
1chmod +x ./bin/*
Missing Styles/JavaScript
If the UI appears broken, ensure you’ve run the collectstatic command:
1karrio collectstatic --noinput
Database Connection Issues
If you’re using PostgreSQL and encounter connection errors, check that the database container is running:
1docker compose -f docker/docker-compose.yml ps