{"library":"nswag","title":"NSwag: OpenAPI Toolchain for .NET and TypeScript","description":"NSwag is a comprehensive toolchain designed for generating OpenAPI (Swagger) specifications from ASP.NET Web API controllers and subsequently generating client code in various languages, primarily TypeScript and C#. It supports a wide range of client frameworks including Angular, React, and jQuery, and handles both OpenAPI 2.0 (Swagger) and 3.0 specifications. The current stable version, 14.7.0, reflects active development with frequent minor releases. NSwag differentiates itself through its deep integration with the .NET ecosystem for robust spec generation and its versatile client generation capabilities. It operates predominantly as a command-line interface (CLI) tool, wrapping .NET executables, and crucially requires a compatible .NET runtime (Full .NET Framework 4.6.2+ or .NET 6.0+) to be installed on the system where the CLI commands are executed.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nswag"],"cli":{"name":"nswag","version":null}},"imports":["nswag <command> [options]","\"node_modules/.bin/nswag\" <command> [options]","nswag openapi2tsclient /input:swagger.json /output:client.ts"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { execSync } from 'child_process';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\n// This quickstart demonstrates how to use the NSwag command-line tool\n// to generate a TypeScript client from an existing OpenAPI (Swagger) specification.\n// Replace 'your-api-swagger.json' with your actual spec file or URL.\n// This assumes nswag is installed globally (`npm install -g nswag`) or available via npx.\n\nconst swaggerSpecPath = path.join(__dirname, 'temp-api-swagger.json');\nconst outputClientPath = path.join(__dirname, 'generated-api-client.ts');\n// Specify the .NET runtime NSwag should use. Common options: Net80, Net90, Net100, WinX64.\n// Ensure a compatible .NET runtime is installed on your system.\nconst runtime = process.env.NSWAG_RUNTIME ?? 'Net80'; \n\n// Create a dummy OpenAPI spec for demonstration if it doesn't exist\nif (!fs.existsSync(swaggerSpecPath)) {\n    console.log(`Creating a dummy OpenAPI spec at ${swaggerSpecPath}`);\n    const dummySpec = {\n        openapi: '3.0.0',\n        info: { title: 'Dummy API', version: '1.0' },\n        paths: {\n            '/items': {\n                get: {\n                    summary: 'Get all items',\n                    responses: {\n                        '200': {\n                            description: 'A list of items',\n                            content: {\n                                'application/json': {\n                                    schema: {\n                                        type: 'array',\n                                        items: { type: 'string' }\n                                    }\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        },\n        components: {}\n    };\n    fs.writeFileSync(swaggerSpecPath, JSON.stringify(dummySpec, null, 2));\n}\n\ntry {\n    console.log(`Generating TypeScript client from ${swaggerSpecPath} using runtime ${runtime}...`);\n    // The 'nswag' command is resolved from PATH (global install) or node_modules/.bin (npx)\n    const command = `npx nswag openapi2tsclient /input:${swaggerSpecPath} /output:${outputClientPath} /runtime:${runtime}`;\n    execSync(command, { stdio: 'inherit' });\n    console.log(`TypeScript client generated successfully to ${outputClientPath}`);\n\n    console.log('\\n--- Generated Client Excerpt ---');\n    const clientContent = fs.readFileSync(outputClientPath, 'utf8');\n    console.log(clientContent.substring(0, 500) + '...\\n'); // Show first 500 chars\n\n} catch (error: any) {\n    console.error('Error generating client:', error.message);\n    console.error('Please ensure NSwag is installed globally (`npm install -g nswag`) or available via npx, and a compatible .NET runtime is installed (e.g., .NET 8 SDK).');\n    process.exit(1);\n} finally {\n    // Clean up dummy spec and generated client\n    if (fs.existsSync(swaggerSpecPath)) fs.unlinkSync(swaggerSpecPath);\n    if (fs.existsSync(outputClientPath)) fs.unlinkSync(outputClientPath);\n    console.log('Cleaned up temporary files.');\n}","lang":"typescript","description":"Demonstrates how to generate a TypeScript API client from an OpenAPI specification using the NSwag command-line tool, including runtime specification and cleanup.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}