# Managing Documents

{% hint style="warning" %}

### Public Data

Remember, all data stored on Context is public. Avoid storing any secrets or private information without additional measures to protect confidentiality.
{% endhint %}

### Fetch Documents

Fetch a specific document or template or asset, from any domain:

```typescript
// Fetch a specific document
const document = await ctx.document("context.startup/link3");  // "domain/path/to/file"
```

### Document Properties

Access and display properties of a document:

```typescript
console.log(document.data.path);
console.log(document.data.versionNumber);
console.log(document.data.data);
console.log(document.data.metadata);
console.log(document.data.templates);
console.log(document.data.type); // Document | Template | Asset
console.log(document.data.txId);
console.log(document.data.createdAt);
console.log(document.data.updatedAt);
console.log(JSON.stringify(document.data));
```

### List Document Versions

Fetch a list of all versions of a document:

```typescript
const documentVersions = await document.data.versions();
```

### Fetch a Specific Document Version

You can fetch a specific version of a document in two different ways:

```typescript
// By using the version method of the document:
const document = await ctx.document("document_path");
const documentVersion = await document.data.getVersion("X.Y.Z");

// By specifying the version directly the document path:
const documentInVersionXYZ = await ctx.document("document_path?v=X.Y.Z");
```

### Create a Document

Steps to create a new document within a domain:

```typescript
const data = YOUR_JSON_DATA;  // JSON data for the document
const templates = ["template_path"];  // Optional array of template paths
const metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" };  // Optional metadata
const documentPath = "mypath/files/document";

const newDocument = await ctx.createDocument(documentPath, data, templates, metadata);
```

### Update a Document

Update an existing document:

```typescript
const updatedData = YOUR_UPDATED_JSON_DATA;  // Updated JSON data
const document_path = "mypath/files/document";

const document = await ctx.document(documentPath);
if (!document.success) {
    // Handle error
}
const result = await document.data.update(updatedData);
```

### Adding Metadata to a Document

You can add metadata to a document using the `addMetadata` method. The metadata object should contain the following (optional) fields: `name`, `description`, and `readme` as shown below:

```typescript
const metadata = { name: "Document Name", description: "Document Description", readme: "ctx:domain/files/my_markdown" };
await document.data.addMetadata(metadata);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ctx.xyz/developers/typescript-sdk/managing-documents.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
