home / skills / stuartf303 / sorcha / yarp

yarp skill

/.claude/skills/yarp

This skill helps configure YARP reverse proxy routes for API gateway routing, enabling path transforms, health mapping, and secure request forwarding.

npx playbooks add skill stuartf303/sorcha --skill yarp

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
3.4 KB
---
name: yarp
description: |
  Configures YARP reverse proxy for API gateway routing.
  Use when: Setting up API gateway routes, configuring path transformations, proxying requests to backend services, or aggregating microservice endpoints.
allowed-tools: Read, Edit, Write, Glob, Grep, Bash, mcp__context7__resolve-library-id, mcp__context7__query-docs
---

# YARP Skill

YARP (Yet Another Reverse Proxy) is the .NET reverse proxy used for API gateway routing in Sorcha. The gateway routes external requests to internal microservices while handling path transformations, security headers, and CORS. Key pattern: gateway-specific endpoints execute BEFORE `MapReverseProxy()` which must be called last.

## Quick Start

### Basic Setup

```csharp
// Program.cs - Add YARP with configuration-based routes
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

// CRITICAL: MapReverseProxy() must be called LAST
app.MapReverseProxy();
```

### Route Configuration

```json
{
  "ReverseProxy": {
    "Routes": {
      "blueprint-route": {
        "ClusterId": "blueprint-cluster",
        "Match": { "Path": "/api/blueprint/{**catch-all}" },
        "Transforms": [{ "PathPattern": "/api/{**catch-all}" }]
      }
    },
    "Clusters": {
      "blueprint-cluster": {
        "Destinations": {
          "destination1": { "Address": "http://blueprint-service:8080" }
        }
      }
    }
  }
}
```

## Key Concepts

| Concept | Usage | Example |
|---------|-------|---------|
| Route | Maps external path to cluster | `"Path": "/api/blueprint/{**catch-all}"` |
| Cluster | Backend service destination(s) | `"Address": "http://service:8080"` |
| Transform | Rewrites path before forwarding | `"PathPattern": "/api/{**catch-all}"` |
| Catch-all | Captures remaining path segments | `{**catch-all}` or `{*any}` |

## Common Patterns

### Path Prefix Stripping

**When:** External API has service prefix, backend doesn't

```json
{
  "Match": { "Path": "/api/blueprint/{**catch-all}" },
  "Transforms": [{ "PathPattern": "/api/{**catch-all}" }]
}
```
Request: `GET /api/blueprint/blueprints` → Backend: `GET /api/blueprints`

### Health Endpoint Mapping

**When:** Unified health checks across services

```json
{
  "blueprint-status-route": {
    "ClusterId": "blueprint-cluster",
    "Match": { "Path": "/api/blueprint/status" },
    "Transforms": [{ "PathPattern": "/api/health" }]
  }
}
```

### X-Forwarded Headers

**When:** Backend needs original client info

```json
"Transforms": [
  { "PathPattern": "/api/{**catch-all}" },
  { "X-Forwarded": "Set" }
]
```

## See Also

- [patterns](references/patterns.md) - Route patterns and transformations
- [workflows](references/workflows.md) - Setup and testing workflows

## Related Skills

- See the **aspire** skill for service discovery integration
- See the **minimal-apis** skill for gateway-specific endpoints
- See the **jwt** skill for authentication pass-through
- See the **docker** skill for container networking

## Documentation Resources

> Fetch latest YARP documentation with Context7.

**How to use Context7:**
1. Use `mcp__context7__resolve-library-id` to search for "yarp"
2. Query with `mcp__context7__query-docs` using the resolved library ID

**Library ID:** `/dotnet/yarp`

**Recommended Queries:**
- "YARP configuration routes clusters transforms"
- "YARP load balancing health checks"
- "YARP request transforms path rewriting"

Overview

This skill configures YARP (Yet Another Reverse Proxy) as an API gateway for routing external requests to internal microservices. It provides route and cluster setup, path transforms, header handling, and patterns for health checks and prefix stripping. Use it to centralize routing, proxy logic, and simple request rewrites in a .NET environment.

How this skill works

The skill loads YARP configuration from app settings and registers the reverse proxy service, then maps the proxy endpoint last in the pipeline to avoid route shadowing. Routes map incoming paths to named clusters, clusters define one or more backend destinations, and transforms rewrite paths or set forwarding headers before the request is proxied. Common transforms include path pattern rewrites and adding X-Forwarded headers to preserve client context.

When to use it

  • Setting up an API gateway to route external traffic to microservices
  • Stripping or rewriting path prefixes so backends receive expected paths
  • Aggregating or proxying requests across multiple service destinations
  • Exposing unified health endpoints for service monitoring
  • Passing original client info to backends via X-Forwarded headers

Best practices

  • Register AddReverseProxy() and load routes from configuration for declarative management
  • Call MapReverseProxy() last so gateway-specific endpoints can run before proxying
  • Use path transforms to strip public prefixes rather than changing backend code
  • Define clusters with multiple destinations for simple load balancing and resilience
  • Add X-Forwarded transforms when backends require client IP or scheme information

Example use cases

  • Route /api/blueprint/{**catch-all} to an internal blueprint service and rewrite the path so backend sees /api/{**catch-all}
  • Expose /api/blueprint/status as a unified health check that maps to /api/health on the service
  • Proxy requests to multiple destinations in a cluster for basic distribution and failover
  • Enable X-Forwarded headers so downstream services can log original client IP and protocol
  • Centralize path rewrite rules in configuration for easier multi-environment deployments

FAQ

What must I remember about MapReverseProxy() placement?

MapReverseProxy() should be called last in the routing setup so any custom gateway endpoints are evaluated first.

How do I remove a service prefix before forwarding?

Add a PathPattern transform that rewrites the incoming path to the backend-expected path, effectively stripping the public prefix.