Skip to content

beerberidie/Sage_agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Sage Business Cloud Accounting Integration - Project Overview

CI/CD Pipeline Python License

Version: 1.0 Date: 2025-10-10 Status: Ready for Implementation Target Region: South Africa


πŸ“‹ Project Documentation

This repository contains comprehensive documentation and a starter codebase for building a production-ready Sage Business Cloud Accounting integration for South African businesses.

πŸ“š Documentation Files

Document Purpose Lines Status
SAGE_API_ANALYSIS.md Complete Sage API v3.1 reference and analysis 1,240 βœ… Complete
MVP_BLUEPRINT.md Detailed implementation guide with code examples 3,760 βœ… Complete
IMPLEMENTATION_CHECKLIST.md Task-by-task checklist for tracking progress 300 βœ… Complete
QUICK_START_GUIDE.md Quick reference with practical code examples 300 βœ… Complete

🎯 Project Goals

Build an MVP that enables:

  1. OAuth 2.0 Authentication with Sage Business Cloud Accounting
  2. Multi-business Support for users with multiple Sage businesses
  3. Sales Invoice Management (create, preview, release)
  4. Contact Management (customers and suppliers)
  5. Audit Logging for compliance and traceability
  6. Incremental Sync for efficient data synchronization
  7. South African Localization (ZAR currency, 15% VAT, 30-day payment terms)

πŸ—οΈ Architecture

Technology Stack

Backend:

  • FastAPI (Python 3.10+)
  • SQLModel + SQLite β†’ PostgreSQL
  • httpx for async HTTP
  • OAuth 2.0 with automatic token refresh

Frontend:

  • React 18 + Vite
  • Modern JavaScript (ES6+)
  • Responsive UI design

Integration:

  • Sage Business Cloud Accounting API v3.1
  • Base URL: https://api.accounting.sage.com/v3.1
  • OAuth: https://www.sageone.com/oauth2/auth/central?filter=apiv3.1

πŸ“– How to Use This Documentation

For Project Managers

  1. Start with: MVP_BLUEPRINT.md - Executive Summary
  2. Review: Implementation Roadmap (5 phases, 10-12 days)
  3. Track progress: IMPLEMENTATION_CHECKLIST.md

For Developers

  1. Start with: SAGE_API_ANALYSIS.md - Understand the Sage API
  2. Read: MVP_BLUEPRINT.md - Complete implementation guide
  3. Reference: QUICK_START_GUIDE.md - Code examples
  4. Track: IMPLEMENTATION_CHECKLIST.md - Check off tasks

For DevOps/Infrastructure

  1. Read: MVP_BLUEPRINT.md - Deployment Strategy section
  2. Review: Configuration Guide section
  3. Implement: Security Considerations section

πŸš€ Quick Start

Prerequisites

Setup (5 minutes)

# 1. Clone repository
git clone <your-repo-url>
cd sage-agent-starter

# 2. Backend setup
cd backend
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -e .
cp .env.example .env
# Edit .env with your Sage credentials

# 3. Frontend setup
cd ../frontend
npm install

# 4. Start development servers
# Terminal 1:
cd backend && uvicorn app.main:app --reload --port 8777

# Terminal 2:
cd frontend && npm run dev

First Steps

  1. Open http://localhost:8081
  2. Click "Connect Sage (OAuth)"
  3. Log in with your Sage trial account
  4. Select your business
  5. Start creating invoices!

Full setup guide: See MVP_BLUEPRINT.md - Configuration Guide section


πŸ“Š Implementation Roadmap

Phase 1: OAuth 2.0 Correction & Token Management ⚠️ CRITICAL

Time: 2-3 days
Priority: CRITICAL

  • Fix OAuth URLs (currently incorrect)
  • Add token expiry tracking
  • Implement automatic token refresh
  • Handle 401 errors with retry logic

Why critical: Without this, authentication will fail completely.


Phase 2: X-Business Context Management ⚠️ CRITICAL

Time: 2 days
Priority: CRITICAL

  • Add business selection after OAuth
  • Store business_id in database
  • Include X-Business header in all API requests

Why critical: Sage API requires X-Business header for all requests.


Phase 3: South African Localization πŸ”₯ HIGH

Time: 2 days
Priority: HIGH

  • Set ZAR as default currency
  • Configure 15% VAT (ZA_STANDARD)
  • Set 30-day payment terms
  • Create invoice template with SA defaults

Why important: Ensures compliance with South African tax requirements.


Phase 4: Audit Logging & Preview/Confirm Pattern πŸ”₯ HIGH

Time: 2 days
Priority: HIGH

  • Implement audit logging for all write operations
  • Add preview/confirm pattern for invoice creation
  • Store request/response data for compliance

Why important: Required for compliance and debugging.


Phase 5: Incremental Sync Mechanism πŸ“Š MEDIUM

Time: 2 days
Priority: MEDIUM

  • Implement sync cursors
  • Use updated_or_created_since parameter
  • Add background sync option

Why important: Reduces API calls and improves performance.


Total Estimated Time: 10-12 days


πŸ” Current State Analysis

βœ… What's Working

  • Basic FastAPI application structure
  • SQLModel database models
  • React frontend with Vite
  • Basic OAuth flow skeleton
  • SageV31Client with invoice/contact methods
  • Settings management

❌ What Needs Fixing (CRITICAL)

  1. OAuth URLs are incorrect (placeholder URLs)

    • Current: https://www.sage.com/oauth/authorize ❌
    • Correct: https://www.sageone.com/oauth2/auth/central?filter=apiv3.1 βœ…
  2. No token refresh mechanism (tokens expire in 5 minutes)

    • Will cause authentication failures after 5 minutes
  3. X-Business header not implemented (commented out)

    • All API requests will fail without this header
  4. No business selection flow

    • Users can't select which Sage business to use
  5. No South African defaults

    • No ZAR currency, no VAT configuration

⚠️ What's Missing (HIGH PRIORITY)

  • Audit logging implementation
  • Preview/confirm pattern for operations
  • Incremental sync mechanism
  • Token encryption (security risk)
  • Rate limiting
  • Production deployment configuration

Full analysis: See MVP_BLUEPRINT.md - Current State Analysis section


πŸ”’ Security Considerations

Critical Security Issues

  1. Tokens stored in plaintext ❌

    • Solution: Implement token encryption (see MVP_BLUEPRINT.md)
  2. CORS allows all origins ❌

    • Current: allow_origins=["*"]
    • Solution: Restrict to specific domains
  3. No rate limiting ❌

    • Solution: Implement rate limiter (see MVP_BLUEPRINT.md)
  4. No input validation ⚠️

    • Solution: Use Pydantic models (partially implemented)

Full security guide: See MVP_BLUEPRINT.md - Security Considerations section


πŸ“¦ Deployment

Development

# Backend
uvicorn app.main:app --reload --port 8777

# Frontend
npm run dev

Production (Docker)

docker-compose up -d

Production (Traditional)

  • Nginx reverse proxy
  • systemd service
  • PostgreSQL database
  • SSL certificate (Let's Encrypt)

Full deployment guide: See MVP_BLUEPRINT.md - Deployment Strategy section


πŸ§ͺ Testing

Unit Tests

cd backend
pytest tests/ -v

Integration Tests

pytest tests/test_oauth_flow.py -v

Manual Testing Checklist

  • OAuth flow works end-to-end
  • Business selection works
  • Invoice creation works
  • Token refresh works (wait 5+ minutes)
  • Audit logging works

Full testing guide: See MVP_BLUEPRINT.md - Testing Strategy section


πŸ“ž Support & Resources

Sage Developer Resources

Project Documentation

  • API Reference: SAGE_API_ANALYSIS.md
  • Implementation Guide: MVP_BLUEPRINT.md
  • Task Checklist: IMPLEMENTATION_CHECKLIST.md
  • Quick Reference: QUICK_START_GUIDE.md

🎯 Success Criteria

MVP is complete when:

  • All documentation created
  • Phase 1-5 implemented
  • All tests passing
  • Security enhancements implemented
  • Production deployment successful
  • User can:
    • Connect Sage account via OAuth
    • Select business
    • Create sales invoices with ZAR and 15% VAT
    • Manage contacts
    • View audit logs
    • Sync data incrementally

πŸ“ˆ Next Steps

  1. Review Documentation

    • Read MVP_BLUEPRINT.md thoroughly
    • Understand the 5-phase roadmap
    • Review code examples
  2. Set Up Development Environment

    • Follow Configuration Guide in MVP_BLUEPRINT.md
    • Register Sage Developer account
    • Get Sage trial account (South Africa)
  3. Start Implementation

    • Begin with Phase 1 (OAuth fixes) - CRITICAL
    • Use IMPLEMENTATION_CHECKLIST.md to track progress
    • Test each phase before moving to next
  4. Deploy to Staging

    • Follow Deployment Strategy in MVP_BLUEPRINT.md
    • Test with real Sage data
    • Get user feedback
  5. Deploy to Production

    • Implement all security enhancements
    • Set up monitoring and logging
    • Go live!

πŸ“ License

[Your License Here]


πŸ‘₯ Contributors

[Your Team Here]


Last Updated: 2025-10-10
Document Version: 1.0
Project Status: Ready for Implementation


Need help? Start with MVP_BLUEPRINT.md - it has everything you need!

About

AI-powered Sage accounting automation agent with OAuth integration and intelligent workflow management

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors