Dart - api()

Creates a new HTTP API.

import 'package:nitric_sdk/nitric.dart';
final publicApi = Nitric.api("public");

Parameters

  • Name
    name
    Required
    Required
    Type
    String
    Description

    The unique name of this API within the app. Subsequent calls to api with the same name will return the same object.

  • Name
    opts
    Optional
    Optional
    Type
    ApiOptions
    Description

    Additional options when creating the API.

    • Name
      basePath
      Optional
      Optional
      Type
      String
      Description

      Base path for all routes in the API.

    • Name
      security
      Optional
      Optional
      Type
      List<OidcOptions>
      Description

      Security rules to apply with scopes to the entire API.

    • Name
      middlewares
      Optional
      Optional
      Type
      List<HttpHandler>
      Description

      The list of middleware that will run before the handler is called. To call the next middleware in the chain use ctx.next(), to finish early return ctx by itself. The ordering of middleware goes: API -> Route -> Method.

OidcRule Parameters

  • Name
    name
    Required
    Required
    Type
    String
    Description

    value must be jwt

  • Name
    issuer
    Required
    Required
    Type
    String
    Description

    the issuer for the JWT tokens e.g. https://account.region.auth0.com

  • Name
    audiences
    Required
    Required
    Type
    List<String>
    Description

    the aud that will be applied to JWT tokens from the issuer.

Examples

Create an API

import 'package:nitric_sdk/nitric.dart';
final publicApi = Nitric.api("public");

Create an API with a base path

If you need to put all the routes in your api below a shared base path, you can do that with the basePath option. In this example we ensure all routes start with /api/v1/ before the route specific path.

import 'package:nitric_sdk/nitric.dart';
final publicApi = Nitric.api("public", opts: ApiOptions(basePath: "/api/v1"));

Apply JWT authentication to an API

import 'package:nitric_sdk/nitric.dart';
// define your security definition
final oidc = Nitric.oidcRule(
"user",
"https://example-issuer.com/.well-known/openid-configuration",
["YOUR-AUDIENCES"]);
final secureApi = Nitric.api(
"secure",
opts: ApiOptions(
security: [
// apply your security rules here, optionally specifying the required scopes.
oidc(["products:read"])
]
)
);
Last updated on Dec 5, 2024