This repository contains the complete source code for the article series published on Medium about building type-safe APIs for Microsoft Dynamics 365 Business Central using TypeSpec and consuming them with React/TanStack Query.
- API-First Development: Define your Business Central API contracts using TypeSpec
- End-to-End Type Safety: From AL/OData endpoints to React frontend
- OpenAPI Generation: Automatically generate OpenAPI 3.0 specifications from TypeSpec
- Type-Safe React: Consume BC APIs with full TypeScript support using TanStack Query
- Automatic Type Generation: Generate TypeScript types directly from OpenAPI specs
- Mock Data Support: Develop against mock Business Central responses
- Node.js 18 or higher
- npm or yarn
# Clone the repository
git clone https://github.com/sn4h/vite-tanstack-typespec-openapi.git
cd vite-tanstack-typespec-openapi
# Install dependencies
npm install
# Build the complete API (TypeSpec → OpenAPI → TypeScript types)
npm run api:build
# Start development server
npm run dev.
├── api-doc/
│ ├── src/
│ │ ├── main.tsp # Main TypeSpec entry point
│ │ └── SalesOrder.tsp # Sales Order API definitions
│ └── tspconfig.yaml # TypeSpec configuration
├── public/
│ ├── definition/
│ │ └── openapi.yaml # Generated OpenAPI specification
│ └── bc-sales-order-data.json # Mock Business Central data
├── src/
│ ├── @types/
│ │ └── openapi.d.ts # Generated TypeScript types
│ ├── lib/
│ │ └── axios-client.ts # Type-safe API client
│ └── routes/
│ └── index.tsx # Main route with TanStack Query
└── package.json
npm run tsp:compile- Compile TypeSpec to OpenAPInpm run tsp:watch- Watch mode for TypeSpec filesnpm run tsp:format- Format TypeSpec filesnpm run api:build- Complete build: TypeSpec → OpenAPI → TypeScript types
npm run dev- Start development server (http://localhost:3000)npm run build- Build for productionnpm run preview- Preview production buildnpm run test- Run testsnpm run lint- Run ESLintnpm run format- Format code with Prettier
- Setting up TypeSpec for Business Central API definitions
- Modeling OData responses with proper type safety
- Generating OpenAPI 3.0 specs automatically
- Implementing
$expandand other OData features
- Generating TypeScript types from OpenAPI specs
- Creating type-safe API clients with openapi-client-axios
- Using TanStack Query for server state management
- Building React components with full type safety
- Working with mock data during development
This project demonstrates modeling the Business Central Sales Order API, including:
- Sales Order headers with essential properties (number, customer, totals)
- Sales Order lines with
$expandsupport for nested data - OData response structures (@odata.context, @odata.count)
- Type-safe query parameters with IntelliSense support
- Mock data for development without Business Central instance
TypeSpec Definition → OpenAPI Spec → TypeScript Types → React Components
When the API changes:
- Update TypeSpec models
- Run
npm run api:build - TypeScript compiler catches breaking changes
- Fix components before deployment
- TypeSpec - API definition language
- TanStack Query - Async state management
- TanStack Router - Type-safe routing
- openapi-client-axios - Type-safe API client
- Vite - Fast build tool
- Tailwind CSS - Utility-first CSS
Found an issue or have a suggestion? Feel free to open an issue or submit a pull request!
MIT
Hans Philip Eide
- Microsoft for creating TypeSpec and Business Central
- The TanStack team for their amazing libraries
- The Business Central developer community
- TypeSpec Documentation
- Business Central API Reference
- TanStack Query Documentation
- TanStack Router Documentation
- OpenAPI Specification
⭐ If you find this project helpful, please consider giving it a star on GitHub!