https://github.com/tryagi/autosdk
Automated .NET SDKs for your APIs
https://github.com/tryagi/autosdk
asyncapi asyncapi-specification automation csharp dotnet generator github-actions incremental-generator nativeaot net8 netframework netstandard nswag openapi sdk source-generator trimming
Last synced: 2 months ago
JSON representation
Automated .NET SDKs for your APIs
- Host: GitHub
- URL: https://github.com/tryagi/autosdk
- Owner: tryAGI
- License: other
- Created: 2023-11-22T04:49:10.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-12T12:03:01.000Z (2 months ago)
- Last Synced: 2025-05-13T01:15:38.439Z (2 months ago)
- Topics: asyncapi, asyncapi-specification, automation, csharp, dotnet, generator, github-actions, incremental-generator, nativeaot, net8, netframework, netstandard, nswag, openapi, sdk, source-generator, trimming
- Language: C#
- Homepage: https://autosdk.net
- Size: 129 MB
- Stars: 55
- Watchers: 6
- Forks: 4
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# AutoSDK
The goal of this project is to automate and minimize the effort of maintaining high-quality .NET SDKs generated based on the OpenAPI specification, mainly to strengthen the AI ecosystem within .NET. The code generated by this library is also actively used in [dozens of our various SDKs of varying complexity levels](#examples-of-use-in-real-sdks) and in [the LangChain.NET project](https://github.com/tryAGI/LangChain), which ensures that it is tested in various scenarios and is ready for them.
Inspired by [NSwag](https://github.com/RicoSuter/NSwag) ❤️.## 🔥Features🔥
- Detects your TargetFramework and generates optimal code for it (including net6.0/net7.0/net8.0 improvements)
- Supports .Net Framework/.Net Standard
- Supports OpenAPI 3.1 specs via internal conversion to OpenAPI 3.0 (includes support for nullability, examples, const)
- Does not contain dependencies for modern versions of dotnet.
- Only System.Text.Json dependency for .Net Framework/.Net Standard
- Any generated methods provide the ability to pass a CancellationToken
- Allows partial generation (models only) or end points filtering
- Available under MIT license for general users and most organizations
- Uses https://github.com/microsoft/OpenAPI.NET for parsing OpenAPI specification
- Supports nullable enable/trimming/native AOT compilation/CLS compliance
- Tested on GitHub 220k lines OpenAPI specification
- Supports OneOf/AnyOf/AllOf/Not schemas
- Supports Enums for System.Text.Json
- Supports SSE (Server-Sent Events) through the `application/x-ndjson` content type
- Uses Incremental Source Generators for efficient generation and caching.
- Efficient O(n) implementation, fully suitable for large/super large OpenAPI specifications
- Used in 10+ real SDKs and adapted to solve various problems# 🚀Quick start🚀
## CLI (Recommended)
You can use the CLI to generate the code.
```bash
dotnet tool install --global autosdk.cli --prerelease
rm -rf Generated
autosdk generate openapi.yaml \
--namespace Namespace \
--clientClassName YourApi \
--targetFramework net8.0 \
--output Generated
```
It will generate the code in the "Generated" subdirectory.
It also will include polyfills for .Net Framework/.Net Standard TargetFrameworks.## Source generator
- Install the package
```bash
dotnet add package AutoSDK.SourceGenerators
```
- Add the following optional settings to your csproj file to customize generation. You can check all settings [here](https://github.com/tryAGI/AutoSDK/blob/main/src/libs/AutoSDK.SourceGenerators/AutoSDK.SourceGenerators.props):
```xml
Ollama
OllamaApi
false
true
true
true
getPet;deletePet
getPet;deletePet
Pet;Model
Pet;Model```
- It's all! Now you can build your project and use the generated code. You also can use IDE to see the generated code in any moment, this is a example for Rider:
# Trimming support
## CLI
CLI generates Trimming/NativeAOT compatible code by default.## Source generator
Since there are two source generators involved, we will have to create a second project so that the generator for the JsonSerializerContext will “see” our models
- Create new project for your models. And disable methods/constructors generation:
```xmlfalse
true
true```
- Reference this project in your main project.
- Add `SourceGenerationContext.cs` file to your main project with the following content:
```csharp
using System.Text.Json.Serialization;namespace Namespace;
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(AutoSDKTrimmableSupport))]
internal sealed partial class SourceGenerationContext : JsonSerializerContext;
```
- Add the following settings to your main csproj file:
```xmlfalse
true
true
Namespace.SourceGenerationContext```
- Add these settings to your new and main csproj file to enable trimming(or use Directory.Build.props file):
```xmltrue
true
true
false
false```
- It's all! Now you can build your project and use the generated code with full trimming/nativeAOT support.## Known Errors
### Generator error: "Could not write to output file 'Path/to/file'. Could not find part of the path"
This error happens if the generated file path is too long. This happens if you didn't activated long path support on windows.
To enable it follow the offical docs:
https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry#registry-setting-to-enable-long-paths
## 📚Examples of use in real SDKs📚
- https://github.com/tryAGI/OpenAI
- https://github.com/tryAGI/Ollama
- https://github.com/tryAGI/Anthropic
- https://github.com/tryAGI/LangSmith
- https://github.com/tryAGI/Replicate
- https://github.com/tryAGI/DeepInfra
- https://github.com/tryAGI/Leonardo
- https://github.com/HavenDV/GitHub.NET