---
description: Choose between the Durable Object Container API and the higher-level Container class.
title: API
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/containers/llms.txt  
> Use this file to discover all available pages before exploring further.

# API

Last updated Aug 28, 2026|Copy as Markdown|[View as Markdown](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/index.md)|[Agent setup](https://codex-container-api-docs.previews.developers.cloudflare.com/agent-setup/)

Containers provide two APIs for managing a container from a Durable Object. Both APIs address the same container runtime.

For new applications, use the Durable Object Container API when you need direct lifecycle control. Use the `Container` class when you prefer built-in lifecycle helpers.

### [Durable Object Container API](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/)

Start, stop, monitor, and connect to a container through `ctx.container`.

### [Container class](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/)

Use a higher-level class built on Durable Objects, with routing, readiness checks, lifecycle hooks, and scheduling.

## Choose an API

### Durable Object Container API

The Durable Object Container API exposes the container runtime through `ctx.container`. Choose it when you need direct control over startup, shutdown, networking, or resource usage. You can add readiness checks, custom request routing, or lifecycle policies when your application needs them.

### Container class

The `Container` class builds on Durable Objects and the runtime API. Choose it when you prefer built-in request proxying, readiness checks, lifecycle hooks, and scheduling. These helpers reduce application code, but some features use Durable Object storage and alarms.

The following table compares both options:

| Requirement                | Durable Object Container API                                                                                                                                                                                                                                                                                                                                                                          | Container class                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Start and stop a container | [start()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#start), [signal()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#signal), and [destroy()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#destroy) | [start()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#start), [stop()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#stop), and [destroy()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#destroy)                                                                                                                                                       |
| Send and proxy traffic     | [getTcpPort(port).fetch()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#gettcpport) and [getTcpPort(port).connect()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#gettcpport)                                                                                         | [fetch()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#fetch) and [containerFetch()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#containerfetch)                                                                                                                                                                                                                                                              |
| Execute another process    | [exec()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#exec)                                                                                                                                                                                                                                                                           | [ctx.container.exec()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#execute-commands)                                                                                                                                                                                                                                                                                                                                                                                 |
| Check port readiness       | Use [getTcpPort()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#gettcpport) in application code                                                                                                                                                                                                                                       | [startAndWaitForPorts()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#startandwaitforports) and [waitForPort()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#waitforport)                                                                                                                                                                                                                                      |
| Handle concurrent starts   | Coordinate calls to [start()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#start) when needed                                                                                                                                                                                                                                         | Handled by [start()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#start) and [startAndWaitForPorts()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#startandwaitforports)                                                                                                                                                                                                                                       |
| Run lifecycle hooks        | [monitor()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#monitor) and application code                                                                                                                                                                                                                                                | [onStart()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#onstart), [onStop()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#onstop), [onError()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#onerror), and [onActivityExpired()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#onactivityexpired) |
| Stop inactive containers   | [setInactivityTimeout()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/#setinactivitytimeout)                                                                                                                                                                                                                                           | [sleepAfter](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#sleepafter) and [onActivityExpired()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#onactivityexpired)                                                                                                                                                                                                                                                |
| Schedule callbacks         | [ctx.storage.setAlarm()](https://codex-container-api-docs.previews.developers.cloudflare.com/durable-objects/api/alarms/#setalarm) (Durable Object API)                                                                                                                                                                                                                                               | [schedule()](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/#schedule)                                                                                                                                                                                                                                                                                                                                                                                                   |

Use the Durable Object Container API for latency-sensitive workloads or workloads that need a smaller storage footprint.

## Use the Durable Object Container API

The Durable Object Container API is available through `ctx.container` of the Durable Object. It exposes the container runtime without adding lifecycle policy.

```ts
import { DurableObject } from "cloudflare:workers";

export class MyContainer extends DurableObject<Env> {
	constructor(ctx: DurableObjectState, env: Env) {
		super(ctx, env);
		ctx.blockConcurrencyWhile(() =>
			ctx.container.setInactivityTimeout(10 * 60 * 1000),
		);
	}

	async fetch(request: Request): Promise<Response> {
		if (!this.ctx.container.running) {
			this.ctx.container.start({ enableInternet: true });
		}

		return this.ctx.container.getTcpPort(8080).fetch(request);
	}
}
```

The `running` property does not indicate port readiness. Check the required port before routing the first request if your process needs time to start.

For all methods, refer to the [Durable Object Container API](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/durable-object-container/).

## Use the Container class

The [Container class ↗](https://github.com/cloudflare/containers) extends `DurableObject`. It adds default routing, readiness checks, lifecycle hooks, activity tracking, and scheduled callbacks.

```ts
import { Container } from "@cloudflare/containers";

export class MyContainer extends Container {
	defaultPort = 8080;
	sleepAfter = "10m";
}
```

These helpers reduce application code. They also add lifecycle state and scheduled work to the Durable Object. For all properties and methods, refer to the [Container class API](https://codex-container-api-docs.previews.developers.cloudflare.com/containers/api/container-class/).

Was this helpful?

YesNo

## On this page

[![](https://codex-container-api-docs.previews.developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://codex-container-api-docs.previews.developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"WebPage","@id":"https://developers.cloudflare.com/containers/api/#page","headline":"API · Cloudflare Containers docs","description":"Choose between the Durable Object Container API and the higher-level Container class.","url":"https://developers.cloudflare.com/containers/api/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-08-28","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
```
