Durable Object ID は、Durable Object を識別するための 64 桁の 16 進数です。64 桁の 16 進数すべてが有効な ID ではありません。Durable Object ID は、DurableObjectNamespace インターフェイス経由で間接的に作ります。
DurableObjectId インターフェイスは、新規または既存の Durable Object を指します。このインターフェイスは、DurableObjectNamespace::get が Durable Object へリクエストを送るための DurableObjectStub を取得するときに、もっともよく使います。Durable Object の ID を作っても、Durable Object 自体は作られません。Durable Object は、DurableObjectId からスタブを作ったあと、遅延して作成されます。実際にアクセスされるまでオブジェクトが構築されないようにするためです。
toString は、DurableObjectId を 64 桁の 16 進文字列に変換します。この文字列はログや、セッション Cookie など別の場所への保存に使えます。この文字列を使うと、DurableObjectNamespace::idFromString で DurableObjectId を再構築できます。
// 新しい一意の ID を作成する
const id = env.MY_DURABLE_OBJECT.newUniqueId();
// ID を文字列に変換し、セッション Cookie など別の場所に保存する
const session_id = id.toString();
...
// 文字列から ID を再作成する
const id = env.MY_DURABLE_OBJECT.idFromString(session_id);- なし。
- 64 桁の 16 進文字列。
equals は、2 つの DurableObjectId インスタンスが等しいかを比較します。
const id1 = env.MY_DURABLE_OBJECT.newUniqueId();
const id2 = env.MY_DURABLE_OBJECT.newUniqueId();
console.assert(!id1.equals(id2), "Different unique ids should never be equal.");id1 = env.MY_DURABLE_OBJECT.newUniqueId()
id2 = env.MY_DURABLE_OBJECT.newUniqueId()
assert not id1.equals(id2), "Different unique ids should never be equal."- 比較対象となる必須の
DurableObjectId。
- 真偽値。等しい場合は true、それ以外は false です。
name は DurableObjectId の任意プロパティで、DurableObjectNamespace::idFromName で DurableObjectId を作ったときに使った名前を返します。DurableObjectNamespace::newUniqueId で作った場合、この値は undefined です。
呼び出し側が idFromName() または getByName() を使うと、Durable Object 内部の ctx.id でも name プロパティを使えます。次の場合、ctx.id.name は undefined になります。
- 呼び出し側が
idFromString()で Durable Object にアクセスした場合。元の ID がidFromName()で作られていても同じです。 - 1,024 バイトを超える名前は
ctx.idに渡りません。 - Durable Object が
newUniqueId()で作られた場合。
const uniqueId = env.MY_DURABLE_OBJECT.newUniqueId();
const fromNameId = env.MY_DURABLE_OBJECT.idFromName("foo");
console.assert(uniqueId.name === undefined, "unique ids have no name");
console.assert(
fromNameId.name === "foo",
"name matches parameter to idFromName",
);const uniqueId: DurableObjectId = env.MY_DURABLE_OBJECT.newUniqueId();
const fromNameId: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName("foo");
console.assert(uniqueId.name === undefined, "unique ids have no name");
console.assert(
fromNameId.name === "foo",
"name matches parameter to idFromName",
);unique_id = env.MY_DURABLE_OBJECT.newUniqueId()
from_name_id = env.MY_DURABLE_OBJECT.idFromName("foo")
assert unique_id.name is None, "unique ids have no name"
assert from_name_id.name == "foo", "name matches parameter to idFromName"同じ name は、Durable Object 内部でも ctx.id.name で使えます。
import { DurableObject } from "cloudflare:workers";
export class ChatRoom extends DurableObject {
async getRoomName() {
return this.ctx.id.name; // "foo" when accessed via getByName("foo")
}
}import { DurableObject } from "cloudflare:workers";
export class ChatRoom extends DurableObject<Env> {
async getRoomName(): Promise<string | undefined> {
return this.ctx.id.name; // "foo" when accessed via getByName("foo")
}
}from workers import DurableObject
class ChatRoom(DurableObject):
async def get_room_name(self):
return self.ctx.id.name # "foo" when accessed via get_by_name("foo")jurisdiction は DurableObjectId の任意プロパティで、その ID が制限されている 管轄(jurisdiction) を返します。例は "eu" や "fedramp" です。同じ値は Durable Object 内部の ctx.id.jurisdiction でも使え、alarm ハンドラー や idFromString() 経由でアクセスしたオブジェクトでも使えます。管轄を引数で渡したり、ストレージに保存したりしなくても、リージョンを意識した判断ができます。
jurisdiction は、次を含むすべての ID 作成経路で保持されます。
- 管轄で制限したサブ名前空間から作った ID。例は
env.MY_DURABLE_OBJECT.jurisdiction("eu").idFromName("foo")や.newUniqueId()です。 env.MY_DURABLE_OBJECT.newUniqueId({ jurisdiction: "eu" })で作った ID。idFromString()で文字列から復元した ID。管轄は文字列自体にエンコードされているため、どの名前空間バインディングでも使えます。
ctx.id.jurisdiction が undefined になるのは、次の 2 つの場合です。
- Durable Object が、管轄で制限した名前空間で作られていない場合。
- Durable Object のアラームが 2026-03-15 より前にスケジュールされた場合。値を補うには、
fetch()または RPC ハンドラーからアラームを再スケジュールします。
const plainId = env.MY_DURABLE_OBJECT.idFromName("foo");
const euId = env.MY_DURABLE_OBJECT.jurisdiction("eu").idFromName("foo");
console.assert(plainId.jurisdiction === undefined, "no jurisdiction set");
console.assert(euId.jurisdiction === "eu", "jurisdiction matches namespace");plain_id = env.MY_DURABLE_OBJECT.idFromName("foo")
eu_id = env.MY_DURABLE_OBJECT.jurisdiction("eu").idFromName("foo")
assert plain_id.jurisdiction is None, "no jurisdiction set"
assert eu_id.jurisdiction == "eu", "jurisdiction matches namespace"