πΈπͺ A comprehensive ASP.NET Core Web API for integrating with Swedish Tax Authority (Skatteverket) VAT Declaration (Momsdeklaration) system.
- β Complete VAT Declaration Management - Draft creation, validation, submission, and retrieval
- β OAuth 2.0 Client Credentials Grant - Secure authentication with Skatteverket
- β Certificate-based Authentication - Organization certificate support
- β Multi-organization Support - Handle declarations for multiple companies
- β Real-time Validation - Business rule validation and error handling
- β Lock Management - Draft locking for signing workflows
- π Enterprise Security - JWT authentication, certificate validation, rate limiting
- π Comprehensive Auditing - Full API call tracking and security event logging
- π‘οΈ Data Protection - Encrypted sensitive data storage and secure string handling
- π Certificate Management - Advanced X.509 certificate validation and chain verification
- π¨ Security Headers - HSTS, CSP, XSS protection, and more
- π Health Monitoring - Memory, disk, and external service health checks
- π Structured Logging - Serilog with correlation ID tracking
- β‘ Performance Optimized - Retry policies, circuit breakers, and caching
- π³ Container Ready - Docker support with multi-stage builds
- π Production Ready - Rate limiting, error handling, and monitoring
- π OpenAPI/Swagger - Complete API documentation with examples
- π§ͺ Input Validation - FluentValidation with Swedish business rules
- π Auto-mapping - AutoMapper integration for clean data transforms
- π¦ Dependency Injection - Clean architecture with service abstractions
Built following Clean Architecture principles with clear separation of concerns:
βββ Controllers/ # API endpoints and HTTP handling
βββ Models/
β βββ DTOs/ # Data transfer objects
β βββ Requests/ # API request models
β βββ Responses/ # API response models
β βββ Validators/ # FluentValidation validators
βββ Services/ # Business logic layer
βββ Authentication/ # OAuth and certificate auth
βββ Configuration/ # App configuration and settings
βββ Middleware/ # Cross-cutting concerns
βββ Utils/ # Utilities and extensions
- .NET 8.0 SDK - Download here
- Skatteverket API Credentials:
- Client ID
- Client Secret
- Organization certificate (.pfx format)
- SSL Certificate (production deployments)
Create appsettings.json or set environment variables:
{
"SkatteverketAPI": {
"UseTestEnvironment": true,
"OAuth": {
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret"
},
"Certificate": {
"Path": "certificates/organization.pfx",
"Password": "your-certificate-password"
}
}
}export SKATTEVERKET_CLIENT_ID="your-client-id"
export SKATTEVERKET_CLIENT_SECRET="your-client-secret"
export CERTIFICATE_PASSWORD="your-cert-password"
export ASPNETCORE_ENVIRONMENT="Production"# Clone and navigate to project
git clone <repository-url>
cd MomsdeklarationAPI
# Restore dependencies
dotnet restore
# Run the application
dotnet run --project MomsdeklarationAPI
# Access Swagger UI
open https://localhost:7000# Using Docker Compose (recommended)
docker-compose up -d
# Or build manually
docker build -t momsdeklaration-api .
docker run -p 8080:8080 momsdeklaration-api| Method | Endpoint | Description |
|---|---|---|
GET |
/api/ping |
Health check ping |
POST |
/api/utkast |
Get multiple drafts |
POST |
/api/utkast/{redovisare}/{period} |
Create/update draft |
GET |
/api/utkast/{redovisare}/{period} |
Get specific draft |
DELETE |
/api/utkast/{redovisare}/{period} |
Delete draft |
POST |
/api/kontrollera/{redovisare}/{period} |
Validate draft |
PUT |
/api/las/{redovisare}/{period} |
Lock for signing |
DELETE |
/api/las/{redovisare}/{period} |
Unlock draft |
POST |
/api/inlamnat |
Get submitted declarations |
GET |
/api/inlamnat/{redovisare}/{period} |
Get specific submission |
POST |
/api/beslutat |
Get decided declarations |
GET |
/api/beslutat/{redovisare}/{period} |
Get specific decision |
| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Application health status |
GET |
/swagger |
API documentation |
{
"momspliktigForsaljning": 100000, // Taxable sales
"momsForsaljningUtgaendeHog": 25000, // Outgoing VAT 25%
"momsForsaljningUtgaendeMedel": 6000, // Outgoing VAT 12%
"momsForsaljningUtgaendeLag": 3000, // Outgoing VAT 6%
"ingaendeMomsAvdrag": 5000, // Input VAT deduction
"summaMoms": 29000 // Total VAT to pay/receive
}{
"momsuppgift": {
"momspliktigForsaljning": 100000,
"momsForsaljningUtgaendeHog": 25000,
"ingaendeMomsAvdrag": 5000,
"summaMoms": 20000
},
"kommentar": "Monthly VAT declaration"
}- Certificate Authentication - Client presents organization certificate
- OAuth Token Exchange - Client credentials grant with Skatteverket
- JWT Token Validation - Bearer token for API requests
- Request Correlation - Unique correlation ID for request tracking
- Rate Limiting - 100 requests per 15 minutes per client
- Input Validation - All inputs validated using FluentValidation
- Audit Logging - All operations logged with user context
- Data Encryption - Sensitive data encrypted at rest
- Security Headers - HSTS, CSP, XSS protection enabled
# Run all tests
dotnet test
# Run with coverage
dotnet test --collect:"XPlat Code Coverage"# Format code
dotnet format
# Analyze code
dotnet analyzeThe application uses test endpoints by default in development:
- Test API base URL:
https://test.app.skatteverket.se/momsdeklaration/v1 - Test OAuth endpoint:
https://test-orgoauth2.skatteverket.se/oauth2/v1/org/token
Set UseTestEnvironment: false for production endpoints.
- Application Health -
/healthendpoint - External Dependencies - Skatteverket API connectivity
- System Resources - Memory, disk space, CPU usage
- Structured Logging - JSON-formatted logs with Serilog
- Correlation Tracking - Request correlation IDs
- Security Events - Authentication and authorization events
- Performance Metrics - Request duration and error rates
- API response times
- Error rates by endpoint
- Authentication success/failure rates
- Certificate validation events
-
Docker (Recommended)
docker-compose -f docker-compose.prod.yml up -d
-
Azure App Service
az webapp create --name momsdeklaration-api --plan myplan az webapp deployment source config-zip --src release.zip -
Traditional Server
dotnet publish -c Release -o ./publish # Copy to server and configure as system service
See DEPLOYMENT.md for detailed deployment instructions.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"SkatteverketAPI": {
"BaseUrl": "https://app.skatteverket.se/momsdeklaration/v1",
"TestBaseUrl": "https://test.app.skatteverket.se/momsdeklaration/v1",
"UseTestEnvironment": false,
"OAuth": {
"ClientId": "${CLIENT_ID}",
"ClientSecret": "${CLIENT_SECRET}",
"TokenEndpoint": "https://orgoauth2.skatteverket.se/oauth2/v1/org/token",
"Scope": "momsdeklaration:read momsdeklaration:write"
},
"Certificate": {
"Path": "/certificates/organization.pfx",
"Password": "${CERT_PASSWORD}",
"ValidationMode": "ChainTrust"
},
"Timeout": 30,
"RetryCount": 3
}
}# Health check
curl http://localhost:5000/health
# Get draft (requires authentication)
curl -X GET "http://localhost:5000/api/utkast/1234567890/202412" \
-H "Authorization: Bearer YOUR_TOKEN"
# Create draft
curl -X POST "http://localhost:5000/api/utkast/1234567890/202412" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"momsuppgift": {
"momspliktigForsaljning": 100000,
"momsForsaljningUtgaendeHog": 25000
}
}'- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow C# coding conventions
- Include unit tests for new features
- Update documentation for API changes
- Ensure security best practices
Certificate Errors:
# Verify certificate
openssl pkcs12 -in certificate.pfx -noout -info
# Check certificate permissions
ls -la certificates/Authentication Issues:
# Check logs for auth errors
grep -i "auth" logs/momsdeklaration-*.txt
# Verify client credentials
curl -X POST "https://test-orgoauth2.skatteverket.se/oauth2/v1/org/token" \
-d "grant_type=client_credentials&client_id=YOUR_ID&client_secret=YOUR_SECRET"API Connectivity:
# Test Skatteverket connectivity
curl https://test.app.skatteverket.se/momsdeklaration/v1/ping- β Swedish Tax Authority (Skatteverket) API Compliance
- β GDPR Data Protection Compliance
- β OAuth 2.0 Security Standards
- β X.509 Certificate Standards
- β OpenAPI 3.0 Specification
- Documentation: See DEPLOYMENT.md for deployment guide
- Issues: Report bugs via GitHub Issues
- Security: Report security issues privately to [email protected]
This project is proprietary software. All rights reserved.
Built with β€οΈ for Swedish businesses using .NET 8.0