Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit ca4da8c

Browse files
committed
Fix API reference examples for middleware, basepath and security.
1 parent 24fc56e commit ca4da8c

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

docs/reference/csharp/api/api.mdx

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,24 @@ Application.Run();
6363
### Create an API with universal middleware
6464

6565
```csharp
66-
using Application = Nitric.Sdk.Application;
66+
using Application = Nitric.Sdk.Nitric;
6767
using Nitric.Sdk.Resource;
68-
69-
private HttpContext ValidateRequest(HttpContext ctx, Func<HttpContext, HttpContext> next)
70-
{
71-
// Validation logic
72-
return next(ctx);
73-
}
68+
using Nitric.Sdk.Service;
7469

7570
var api = Application.Api("main", new ApiOptions(
76-
Middleware: new Middleware<HttpContext>[] { ValidateRequest }
71+
middleware: new Middleware<HttpContext>[] {
72+
(context, next) => {
73+
return next(context);
74+
}
75+
}
7776
));
7877

78+
api.Get("/example", (ctx) =>
79+
{
80+
ctx.Res.Text("Hello World!");
81+
return ctx;
82+
});
83+
7984
Application.Run();
8085
```
8186

@@ -84,47 +89,53 @@ Application.Run();
8489
If you need to put all the routes in your api below a shared base path, you can do that with the `BaseRoute` option. In this example we ensure all routes start with `/api/v1/` before the route specific path.
8590

8691
```csharp
87-
using Application = Nitric.Sdk.Application;
92+
using Application = Nitric.Sdk.Nitric;
8893
using Nitric.Sdk.Resource;
8994

9095
var api = Application.Api("main", new ApiOptions(
91-
BaseRoute: "/api/v1"
96+
basePath: "/api/v1"
9297
));
9398

99+
// Define API routes
100+
api.Get("/example", (ctx) =>
101+
{
102+
ctx.Res.Text("Hello World!");
103+
return ctx;
104+
});
105+
94106
Application.Run();
95107
```
96108

97109
### Apply JWT authentication to an API
98110

99111
```csharp
100112
using Application = Nitric.Sdk.Nitric;
101-
using System.Collections.Generic;
102113
using Nitric.Sdk.Resource;
114+
using System.Collections.Generic;
115+
116+
// Define security configuration
117+
var securityOptions = new OidcOptions[]
118+
{
119+
new OidcOptions(
120+
name: "user",
121+
audiences: ["YOUR_AUDIENCES"],
122+
issuer: "https://example-issuer.com",
123+
// Optionally apply required scopes to this api
124+
// in this case users will require the products:read scope to access the API
125+
scopes: new string[] { "products:read" }
126+
)
127+
};
103128

104129
var secureApi = Application.Api("main", new ApiOptions(
105-
// You can optionally apply security rules to the entire API
106-
Security: new Dictionary<string, string[]>
107-
{
108-
// apply the 'user security definition the whole API'
109-
{
110-
"user",
111-
// Optionally apply required scopes to this api
112-
// in this case users will require the products:read scope to access the API
113-
new string[] { "products:read" }
114-
},
115-
},
116-
// security requirements for your API are defined here
117-
SecurityDefinitions: new Dictionary<string, SecurityDefinition>
118-
{
119-
// define a security definition called 'user'
120-
{ "user",
121-
new JwtSecurityDefinition(
122-
'https://example-issuer.com',
123-
new string[] { "YOUR_AUDIENCES" }
124-
)
125-
}
126-
}
130+
security: securityOptions
127131
));
128132

133+
// Define API routes
134+
api.Get("/example", (ctx) =>
135+
{
136+
ctx.Res.Text("Hello, Secure World!");
137+
return ctx;
138+
});
139+
129140
Application.Run();
130141
```

0 commit comments

Comments
 (0)