openapi-angular is a type-safe HTTP client for Angular that leverages your OpenAPI schema to provide fully typed API interactions.
Inspired by OpenAPI Fetch, but specifically adapted for Angular's
HttpClient
.
- Type-safe requests and responses
- Path parameter validation
- Autocompletion for API endpoints
-
Angular DI ready (uses
HttpClient
under the hood) - Lightweight proxy interface to
HttpClient
npm install openapi-angular
- First, generate your OpenAPI types using openapi-typescript:
npx openapi-typescript ./path-to-your-schema.yaml -o ./src/app/shared/api/types.ts
- Create your service:
import { injectOpenapiClient } from "openapi-angular";
import { paths } from "./my-openapi-3-schema"; // generated types
@Injectable({ providedIn: "root" })
export class BlogService {
private readonly client = injectOpenapiClient<paths>({
baseUrl: "https://myapi.dev/v1/",
});
// Get a post with type-safe params and response
getPost(postId: string) {
return this.client.get("/blogposts/{post_id}", {
params: {
path: { post_id: postId }, // ✅ Type-checked path params
query: { version: 2 }, // ✅ Type-checked query params
},
});
// Returns Observable<{ title: string; content: string }>
}
// Create a post with type-safe body
createPost(title: string, content: string) {
return this.client.post("/blogposts", {
title,
content,
publishedAt: new Date().toISOString(),
});
// Returns Observable<{ id: string; title: string }>
}
}
- Path-based URLs: Specify OpenAPI paths instead of full URLs
// Instead of:
http.get("https://myapi.dev/v1/blogposts/123");
// You write:
client.get("/blogposts/{post_id}", {
baseUrl: "https://myapi.dev/v1/",
params: { path: { post_id: "123" } },
});
- Structured Params: All parameters are organized by type:
{
params: {
path: { post_id: string },
query: { version?: number },
header: { 'X-Request-ID': string }
}
}
- Base URL Management: Set a default but override per-request:
// Set default:
const client = injectOpenapiClient({ baseUrl: "https://api.example.com/v1" });
// Override:
client.get("/endpoint", { baseUrl: "https://backup.example.com" });
- Response Typing: Automatic typing for application/json responses
The client provides proper typing for successful responses, but you should handle errors:
this.blogService.getPost("123").pipe(
catchError((error: HttpErrorResponse) => {
console.error("API Error:", error);
return throwError(() => new Error("Failed to load post"));
})
);
-
❌ No support for JSONP requests
-
❌ Cookie parameters not yet implemented
-
❌ Non-JSON response bodies lose type safety (use responseType options carefully)
-
Validate responses: Consider using zod or other validation libraries for runtime type checking
-
Handle errors globally: Implement an HTTP interceptor for consistent error handling
Found an issue? Want to improve the library? We welcome contributions!
-
Fork the repository
-
Create a feature branch
-
Submit a pull request
This project is a modified version of openapi-fetch, adapted specifically for Angular's HttpClient
.
Both projects are licensed under MIT.