> ## Documentation Index
> Fetch the complete documentation index at: https://nestrs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# NestFactory: bootstrap your nestrs application

> Reference for all NestFactory methods that create HTTP applications, microservice servers, and hybrid processes across every transport adapter.

`NestFactory` is the entry point for every nestrs application. You call one of its static methods with a root module type parameter, and it builds the `ProviderRegistry`, wires controllers into an Axum `Router`, and returns either a `NestApplication` builder or a `MicroserviceApplication` handle that you configure and then start listening.

```rust theme={null}
use nestrs::prelude::*;

#[module]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create::<AppModule>()
        .listen(3000)
        .await;
}
```

## `NestFactory::create`

Creates an HTTP application from a static root module type.

```rust theme={null}
pub fn create<M: Module>() -> NestApplication
```

<ParamField path="M" type="impl Module">
  The root module type. Must implement the `Module` trait, which is generated by `#[module(...)]`.
</ParamField>

`M::build()` runs at call time: it walks the module import graph, registers all providers into a `ProviderRegistry`, and registers all controllers into an Axum `Router`. The returned `NestApplication` is a builder; no network socket is opened until you call `listen`, `listen_graceful`, or `listen_with_shutdown`.

```rust theme={null}
use nestrs::prelude::*;

#[derive(Default)]
#[injectable]
struct AppState;

#[controller(prefix = "/health")]
struct HealthController;

#[routes(state = AppState)]
impl HealthController {
    #[get("/")]
    async fn ping() -> &'static str { "ok" }
}

#[module(controllers = [HealthController], providers = [AppState])]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create::<AppModule>()
        .listen(3000)
        .await;
}
```

## `NestFactory::create_with_modules`

Creates an HTTP application from a static root module plus a collection of runtime-selected `DynamicModule` instances.

```rust theme={null}
pub fn create_with_modules<M, I>(dynamic_modules: I) -> NestApplication
where
    M: Module,
    I: IntoIterator<Item = DynamicModule>,
```

<ParamField path="M" type="impl Module">
  The static root module type.
</ParamField>

<ParamField path="dynamic_modules" type="IntoIterator<Item = DynamicModule>">
  Zero or more dynamic modules to merge into the root module's registry and router.
</ParamField>

Use this when you need to conditionally include feature routers or plug-in modules that are only known at runtime—for example, when a feature flag is read from the environment after startup.

```rust theme={null}
use nestrs::prelude::*;

#[module]
struct AppModule;

#[module(controllers = [], providers = [])]
struct AdminModule;

#[tokio::main]
async fn main() {
    let dynamic = if std::env::var("ENABLE_ADMIN").is_ok() {
        vec![DynamicModule::from_module::<AdminModule>()]
    } else {
        vec![]
    };

    NestFactory::create_with_modules::<AppModule, _>(dynamic)
        .listen(3000)
        .await;
}
```

## `NestFactory::create_microservice` (TCP)

Creates a microservice application using the TCP transport adapter. Requires the `microservices` feature.

```rust theme={null}
pub fn create_microservice<M>(options: TcpMicroserviceOptions) -> MicroserviceApplication
where
    M: Module + MicroserviceModule,
```

<ParamField path="M" type="impl Module + MicroserviceModule">
  The root module type. Must also implement `MicroserviceModule`, generated when you declare `microservices = [...]` in `#[module]`.
</ParamField>

<ParamField path="options" type="TcpMicroserviceOptions">
  TCP server configuration (host, port, and related settings).
</ParamField>

```rust theme={null}
use nestrs::prelude::*;
use nestrs::microservices::TcpMicroserviceOptions;

#[module]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create_microservice::<AppModule>(
        TcpMicroserviceOptions::default(),
    )
    .listen()
    .await;
}
```

<Tip>
  Call `.also_listen_http(port)` on the returned `MicroserviceApplication` to run both an HTTP router and the microservice transport in a single process (hybrid mode).
</Tip>

## `NestFactory::create_microservice_nats`

Creates a microservice application using the NATS transport adapter. Requires the `microservices-nats` feature.

```rust theme={null}
pub fn create_microservice_nats<M>(options: NatsMicroserviceOptions) -> MicroserviceApplication
where
    M: Module + MicroserviceModule,
```

<ParamField path="options" type="NatsMicroserviceOptions">
  NATS connection and subject configuration.
</ParamField>

```toml theme={null}
# Cargo.toml
[dependencies]
nestrs = { version = "*", features = ["microservices", "microservices-nats"] }
```

```rust theme={null}
use nestrs::prelude::*;
use nestrs::microservices::NatsMicroserviceOptions;

#[module]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create_microservice_nats::<AppModule>(
        NatsMicroserviceOptions::default(),
    )
    .listen()
    .await;
}
```

## `NestFactory::create_microservice_redis`

Creates a microservice application using the Redis Pub/Sub transport adapter. Requires the `microservices-redis` feature.

```rust theme={null}
pub fn create_microservice_redis<M>(options: RedisMicroserviceOptions) -> MicroserviceApplication
where
    M: Module + MicroserviceModule,
```

<ParamField path="options" type="RedisMicroserviceOptions">
  Redis connection and channel configuration.
</ParamField>

```rust theme={null}
use nestrs::prelude::*;
use nestrs::microservices::RedisMicroserviceOptions;

#[module]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create_microservice_redis::<AppModule>(
        RedisMicroserviceOptions::default(),
    )
    .listen()
    .await;
}
```

## `NestFactory::create_microservice_grpc`

Creates a microservice application using the gRPC transport adapter. Requires the `microservices-grpc` feature.

The gRPC transport carries JSON-encoded payloads (`pattern` + `payload_json`) inside protobuf, matching the `nestrs-microservices::wire` module's request/response shapes.

```rust theme={null}
pub fn create_microservice_grpc<M>(options: GrpcMicroserviceOptions) -> MicroserviceApplication
where
    M: Module + MicroserviceModule,
```

<ParamField path="options" type="GrpcMicroserviceOptions">
  Bind address and TLS configuration for the tonic gRPC server.
</ParamField>

```rust theme={null}
use nestrs::prelude::*;
use nestrs::microservices::GrpcMicroserviceOptions;

#[module]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create_microservice_grpc::<AppModule>(
        GrpcMicroserviceOptions::default(),
    )
    .listen()
    .await;
}
```

## `NestFactory::create_microservice_rabbitmq`

Creates a microservice application using the RabbitMQ transport adapter. Requires the `microservices-rabbitmq` feature.

```rust theme={null}
pub fn create_microservice_rabbitmq<M>(
    options: RabbitMqMicroserviceOptions,
) -> MicroserviceApplication
where
    M: Module + MicroserviceModule,
```

<ParamField path="options" type="RabbitMqMicroserviceOptions">
  AMQP connection URL, exchange, and queue configuration.
</ParamField>

```rust theme={null}
use nestrs::prelude::*;
use nestrs::microservices::RabbitMqMicroserviceOptions;

#[module]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create_microservice_rabbitmq::<AppModule>(
        RabbitMqMicroserviceOptions::default(),
    )
    .listen()
    .await;
}
```

## `MicroserviceApplication` builder methods

All `create_microservice_*` methods return a `MicroserviceApplication`. It exposes the following builder methods before you call `listen`:

| Method                         | Description                                                                 |
| ------------------------------ | --------------------------------------------------------------------------- |
| `.also_listen_http(port: u16)` | Run an HTTP router alongside the microservice transport in the same process |
| `.configure_http(f)`           | Apply `NestApplication` builder methods before HTTP starts                  |
| `.get::<T>()`                  | Resolve a provider from the DI container (returns `Arc<T>`)                 |

```rust theme={null}
use nestrs::prelude::*;
use nestrs::microservices::TcpMicroserviceOptions;

#[module]
struct AppModule;

#[tokio::main]
async fn main() {
    NestFactory::create_microservice::<AppModule>(TcpMicroserviceOptions::default())
        .also_listen_http(3001)
        .configure_http(|app| {
            app.set_global_prefix("api")
               .enable_health_check("/health")
        })
        .listen()
        .await;
}
```
