# Moderate assets

An asset is content submitted to CalmLens for moderation. You can create one from a file's bytes, from inline content, from a remote URL, or in sequential parts for larger uploads. This guide covers how to send each kind of content, how to wait for the decision, the limits that apply and the optional processing features. For every field and error code, see the [Assets API reference](/docs/api/assets).

Requests authenticate with a project API key sent from your server. See [Authentication](/docs/guides/authentication).

## Upload with an SDK

For files, pass a file, buffer or stream to your SDK. It selects the upload mode and handles authentication.

**python**

```python
import os
from calmlens import CalmLensServerClient

client = CalmLensServerClient(
    api_key=os.environ["CALMLENS_API_KEY"],
    project_id=os.environ["CALMLENS_PROJECT_ID"],
)

with open("cat.jpg", "rb") as content:
    asset = client.assets.create({
        "name": "cat.jpg",
        "visibility": "private",
        "processingOptions": {"keepContentAfterProcessing": True},
        "content": {"data": content, "type": "image/jpeg"},
    })
print(asset["id"])
```

**go**

```go
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    calmlens "github.com/Censkh/CalmLens/packages/go-sdk"
)

func main() {
    client, err := calmlens.NewServerClient(calmlens.Options{APIKey: os.Getenv("CALMLENS_API_KEY"), ProjectID: os.Getenv("CALMLENS_PROJECT_ID")})
    if err != nil { log.Fatal(err) }
    ctx := context.Background()
    content, err := os.Open("cat.jpg")
    if err != nil { log.Fatal(err) }
    defer content.Close()
    asset, err := client.Assets.Create(ctx, calmlens.SubmitAssetOptions{Name: "cat.jpg", Visibility: "private", ProcessingOptions: &calmlens.ProcessingOptions{KeepContentAfterProcessing: calmlens.Ptr(true)}, Content: &calmlens.Content{Data: content, Type: "image/jpeg"}})
    if err != nil { log.Fatal(err) }
    fmt.Println(asset.ID)
}

```

**JavaScript**

```javascript
import { readFile } from "node:fs/promises";
import { CalmLensServerClient } from "@calmlens/sdk/server";

const client = new CalmLensServerClient({
  apiKey: process.env.CALMLENS_API_KEY!,
  projectId: process.env.CALMLENS_PROJECT_ID!,
});

const asset = await client.assets.create({
  name: "cat.jpg",
  visibility: "private",
  processingOptions: { keepContentAfterProcessing: true },
  content: {
    buffer: await readFile("./cat.jpg"),
    type: "image/jpeg",
  },
});
```

**ruby**

```ruby
require 'calmlens'
client = CalmLens::ServerClient.new(api_key: ENV.fetch('CALMLENS_API_KEY'), project_id: ENV.fetch('CALMLENS_PROJECT_ID'))
asset = client.assets.create(name: 'cat.jpg', content: {data: File.binread('cat.jpg'), type: 'image/jpeg'})
puts asset['id']
client.close
```

**rust**

```rust
use calmlens::{models::*, ServerClient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ServerClient::new(std::env::var("CALMLENS_API_KEY")?, std::env::var("CALMLENS_PROJECT_ID")?)?;
    let bytes = std::fs::read("cat.jpg")?;
    let asset = client.assets().create(SubmitAsset::new("cat.jpg", Content::bytes(bytes, "image/jpeg"))).await?;
    println!("{}", asset.id);
    Ok(())
}
```

**java**

```java
import com.calmlens.CalmLensServerClient;
import com.calmlens.Models.*;

public class Example {
  public static void main(String[] args) throws Exception {
    var client = new CalmLensServerClient(System.getenv("CALMLENS_API_KEY"), System.getenv("CALMLENS_PROJECT_ID"));
    byte[] bytes = java.nio.file.Files.readAllBytes(java.nio.file.Path.of("cat.jpg"));
    Asset asset = client.assets.create(new SubmitAsset("cat.jpg", Content.bytes(bytes, "image/jpeg")).visibility(Visibility.PRIVATE).processingOptions(new ProcessingOptions().keepContentAfterProcessing(true)));
    System.out.println(asset.id());
  }
}
```

**curl**

```sh
curl -X POST "https://api.calmlens.com/v1/projects/$CALMLENS_PROJECT_ID/assets?name=cat.jpg&contentType=image%2Fjpeg&queue=true&visibility=private&processingOptions=%7B%22keepContentAfterProcessing%22%3Atrue%7D" \
  -H "X-Api-Key: $CALMLENS_API_KEY" \
  -H "X-CalmLens-Upload: content" \
  -H "Content-Type: image/jpeg" \
  --data-binary @cat.jpg
```

## Waiting for the decision

Choose either flow independently of the upload mode:

| Flow | SDK | Direct HTTP | Result |
| --- | --- | --- | --- |
| Moderate in place | `client.assets.moderate()` | `queue=false` (the default) | Wait for classification and read the decision from the response. |
| Queue moderation | `client.assets.create()` | `queue=true` | Receive an asset ID while classification runs, then get the decision through an [`asset.classified`](/docs/api/events#event-asset-classified) webhook. |

Text and images support both flows. Audio and video always run asynchronously and return `pending`, even with `queue=false`. A multipart upload uses the queue mode chosen when it starts: CalmLens saves it with the upload, so whichever client sends the final part gets the same behaviour. See [Queue mode](/docs/guides/multipart-uploads#queue-mode).

For queued uploads, save the asset ID and [configure a webhook](/docs/guides/webhooks) for [`asset.classified`](/docs/api/events#event-asset-classified) and [`asset.failed`](/docs/api/events#event-asset-failed). Keep content unpublished until `approved`; a successful upload alone is not a moderation decision.

Completed assets use the nested report described in [Report format](/docs/guides/moderation-reports). Check each category's `status` before reading `score`.

See [Asset lifecycle](/docs/guides/asset-lifecycle) for each status and how to wait for a queued asset, and [Delivering assets](/docs/guides/delivering-assets) to serve asset content, create signed URLs for private assets, and apply lenses.

### Fallback when webhooks are unavailable

If your integration cannot receive webhooks, retrieve the asset periodically with `client.assets.retrieve({ assetId })`. This requires a retry loop in your application; the retrieval call does not wait for classification.

Start with a short delay, increase it between checks using exponential backoff, and cap the interval. Set an overall timeout so checks cannot continue indefinitely. Stop when the status is `approved`, `rejected` or `error`; keep content unpublished while it is `pending`. If the timeout expires, treat the result as unresolved and keep the content unpublished.

`client.assets.retrieve({ assetId })`

## Upload modes

Every upload mode uses [`POST /projects/:projectId/assets`](/docs/api/assets#create-asset). The request body decides the mode:

| You have | Use | Request body |
| --- | --- | --- |
| A file, buffer or stream | [Direct binary upload](/docs/guides/moderating-assets#direct-binary-upload) | The file's bytes, with `X-CalmLens-Upload: content` |
| Base64 content or bytes inside a JSON request | [JSON upload](/docs/guides/moderating-assets#json-upload-with-base64-content) | JSON with `content.base64` |
| Text you have already extracted | [Plain text upload](/docs/guides/moderating-assets#plain-text-upload) | JSON with `content.plainText` |
| A public web page or file URL | [Remote URL upload](/docs/guides/moderating-assets#remote-url-upload) | JSON with `url` |
| A large file, or one you produce in chunks | [Multipart upload](/docs/guides/moderating-assets#multipart-upload) | The first part's bytes, with `X-CalmLens-Upload: part`, then [`POST /assets/:assetId/part`](/docs/api/assets#upload-asset-part) for the rest |

The SDKs choose the mode for you: they stream files with direct binary or multipart uploads and send other inputs as JSON.

### Direct binary upload

For direct HTTP integrations, send the unencoded file as the request body with the file's `Content-Type` and the `X-CalmLens-Upload: content` header. Asset fields go in query parameters. The header is what marks the body as a file, so a JSON file is uploaded as a file rather than read as a request. See [`POST /projects/:projectId/assets`](/docs/api/assets#upload-asset-file) for the complete request.

### JSON upload

Without the `X-CalmLens-Upload` header, the body is JSON: base64 content, plain text or a remote URL.

- Direct image uploads must be smaller than <code>10MB</code>.
- All other file types support up to <code>5GB</code>.
- The request body can contain `content.base64`, `content.plainText`, or `url`.

#### JSON upload with base64 content

```bash
curl -X POST "https://api.calmlens.com/v1/projects/your-project-id/assets?queue=true" \
  -H "X-Api-Key: your-api-key" \
  -H "Content-Type: application/json" \
  --data '{
  "name": "cat.png",
  "description": "Homepage hero image",
  "visibility": "private",
  "processingOptions": {
    "keepContentAfterProcessing": true
  },
  "content": {
    "base64": "your-base64-file",
    "type": "image/png"
  }
}'
```

#### Plain text upload

```bash
curl -X POST "https://api.calmlens.com/v1/projects/your-project-id/assets?queue=true" \
  -H "X-Api-Key: your-api-key" \
  -H "Content-Type: application/json" \
  --data '{
  "name": "notes.txt",
  "content": {
    "plainText": "This is the extracted text to moderate.",
    "type": "text/plain"
  }
}'
```

#### Remote URL upload

Remote URLs must use HTTP or HTTPS. Private IP addresses, localhost names and URL credentials are rejected, and each redirect is checked. Downloads time out after 30 seconds and are limited to 10 MB for images and 20 MB for other files. Use multipart upload for larger files.

```bash
curl -X POST "https://api.calmlens.com/v1/projects/your-project-id/assets?queue=true" \
  -H "X-Api-Key: your-api-key" \
  -H "Content-Type: application/json" \
  --data '{
  "name": "landing-page",
  "url": "https://example.com"
}'
```

### Multipart upload

The SDK chooses multipart uploads when needed. For manual chunks, ordering, finalisation and recovery, see [Multipart uploads](/docs/guides/multipart-uploads).

## Retrying uploads

Each successful upload creates an asset that counts toward your usage, and a request that timed out may still have succeeded. Send an `Idempotency-Key` header with a unique value, such as a UUID, and reuse it when you retry: if the first request succeeded, CalmLens returns its response instead of creating a second asset. An `externalId` is also unique within a project, so a retry that reuses one is refused with [`409 asset/duplicate-external-id`](/docs/api/assets#post-asset-error-asset-duplicate-external-id) rather than creating a duplicate. See [Retrying requests safely](/docs/guides/error-handling#retrying-requests-safely).

## Asset types and limits

CalmLens detects the asset type from the content's MIME type.

<table>
  <thead>
    <tr>
      <th>Asset type</th>
      <th>How it is detected</th>
      <th>Typical input</th>
      <th>Upload limit</th>
      <th>Guide</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td><code>image</code></td>
      <td><code>image/\*</code> MIME types</td>
      <td>PNG, JPEG, WebP, GIF, AVIF</td>

      <td>
        <code>
          20MB
        </code>
      </td>

      <td><a href="/docs/guides/ocr">OCR and image reports</a></td>
    </tr>

    <tr>
      <td><code>video</code> (beta)</td>
      <td><code>video/\*</code> MIME types</td>
      <td>MP4, MOV, WebM</td>

      <td>
        <code>
          5GB
        </code>
      </td>

      <td><a href="/docs/guides/audio-and-video">Audio and video</a></td>
    </tr>

    <tr>
      <td><code>audio</code> (beta)</td>
      <td><code>audio/\*</code> MIME types</td>
      <td>MP3, WAV, M4A</td>

      <td>
        <code>
          5GB
        </code>
      </td>

      <td><a href="/docs/guides/audio-and-video">Audio and video</a></td>
    </tr>

    <tr>
      <td><code>plaintext-document</code></td>
      <td>Plain text MIME types</td>
      <td>TXT, JSON, CSV, YAML</td>

      <td>
        <code>
          5GB
        </code>
      </td>

      <td><a href="/docs/guides/moderation-reports">Reading reports</a></td>
    </tr>

    <tr>
      <td><code>multimedia-document</code></td>
      <td>Supported document MIME types</td>
      <td>PDF, DOCX, DOC, RTF, ODT</td>

      <td>
        <code>
          5GB
        </code>
      </td>

      <td><a href="/docs/guides/moderation-reports">Reading reports</a></td>
    </tr>

    <tr>
      <td><code>website</code></td>
      <td><code>text/html</code> MIME type or URL upload</td>
      <td>HTML or remote website URL</td>

      <td>
        <code>
          5GB
        </code>
      </td>

      <td><a href="/docs/guides/moderation-reports">Reading reports</a></td>
    </tr>

    <tr>
      <td><code>file</code></td>
      <td>Fallback for other file types</td>
      <td>Other binary files</td>

      <td>
        <code>
          5GB
        </code>
      </td>

      <td><a href="/docs/guides/moderation-reports">Reading reports</a></td>
    </tr>
  </tbody>
</table>

Image uploads are also constrained by a maximum image dimension of `12,000 x 12,000` pixels.

## Processing features

Optional analyses are chosen per upload in `processingOptions.features`, keyed by feature. Each feature takes an object with `enabled` and any options of its own. There is no project setting for them: a feature runs for an asset only when the request that uploaded it asks for it.

| Feature | What it does | Stage | Options | Billed per image |
| --- | --- | --- | --- | --- |
| [`ocr`](/docs/guides/ocr) | Reads text in images into a linked text asset and moderates it. | Generally available | `enabled` | OCR |
| [`personDetection`](/docs/guides/people-and-faces) | Locates people in images. | Beta | `enabled` | Person detection |
| [`faceDetection`](/docs/guides/people-and-faces) | Locates faces, with an estimated age range for each when `ageEstimation` is `true`. | Beta | `enabled`, `ageEstimation` | Face detection |

**python**

```python
import os
from calmlens import CalmLensServerClient

client = CalmLensServerClient(
    api_key=os.environ["CALMLENS_API_KEY"],
    project_id=os.environ["CALMLENS_PROJECT_ID"],
)

with open("photo.jpg", "rb") as content:
    asset = client.assets.moderate({
        "name": "photo.jpg",
        "content": {"data": content, "type": "image/jpeg"},
        "processingOptions": {
            "features": {
                "personDetection": {"enabled": True},
                "faceDetection": {"enabled": True, "ageEstimation": True},
            },
        },
    })

report = asset.get("report")
faces = report.get("features", {}).get("faceDetection") if report else None
if faces and faces["status"] == "evaluated":
    for face in faces["faces"]:
        print(face["box"], face.get("ageRange"))
```

**go**

```go
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    calmlens "github.com/Censkh/CalmLens/packages/go-sdk"
)

func main() {
    client, err := calmlens.NewServerClient(calmlens.Options{APIKey: os.Getenv("CALMLENS_API_KEY"), ProjectID: os.Getenv("CALMLENS_PROJECT_ID")})
    if err != nil { log.Fatal(err) }
    content, err := os.Open("photo.jpg")
    if err != nil { log.Fatal(err) }
    defer content.Close()
    asset, err := client.Assets.Moderate(context.Background(), calmlens.SubmitAssetOptions{
        Name:    "photo.jpg",
        Content: &calmlens.Content{Data: content, Type: "image/jpeg"},
        ProcessingOptions: &calmlens.ProcessingOptions{Features: &calmlens.ProcessingFeatures{
            PersonDetection: &calmlens.PersonDetectionFeatureOptions{Enabled: true},
            FaceDetection:   &calmlens.FaceDetectionFeatureOptions{Enabled: true, AgeEstimation: calmlens.Ptr(true)},
        }},
    })
    if err != nil { log.Fatal(err) }
    if asset.Report != nil && asset.Report.Features != nil && asset.Report.Features.FaceDetection != nil {
        for _, face := range asset.Report.Features.FaceDetection.Faces {
            fmt.Println(face.Box, face.AgeRange)
        }
    }
}

```

**JavaScript**

```javascript
import { readFile } from "node:fs/promises";
import { CalmLensServerClient } from "@calmlens/sdk/server";

const client = new CalmLensServerClient({
  apiKey: process.env.CALMLENS_API_KEY!,
  projectId: process.env.CALMLENS_PROJECT_ID!,
});

const asset = await client.assets.moderate({
  name: "photo.jpg",
  content: { buffer: await readFile("./photo.jpg"), type: "image/jpeg" },
  processingOptions: {
    features: {
      personDetection: { enabled: true },
      faceDetection: { enabled: true, ageEstimation: true },
    },
  },
});

const faces = asset.report?.features?.faceDetection;
if (faces?.status === "evaluated") {
  for (const face of faces.faces) console.log(face.box, face.ageRange);
}
```

**ruby**

```ruby
require 'calmlens'

client = CalmLens::ServerClient.new(api_key: ENV.fetch('CALMLENS_API_KEY'), project_id: ENV.fetch('CALMLENS_PROJECT_ID'))
asset = client.assets.moderate(
  name: 'photo.jpg',
  content: {data: File.binread('photo.jpg'), type: 'image/jpeg'},
  processing_options: {features: {
    person_detection: {enabled: true},
    face_detection: {enabled: true, age_estimation: true}
  }}
)
faces = asset.dig('report', 'features', 'faceDetection')
faces['faces'].each { |face| puts face['box'], face['ageRange'] } if faces && faces['status'] == 'evaluated'
client.close
```

**rust**

```rust
use calmlens::{models::*, ServerClient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ServerClient::new(std::env::var("CALMLENS_API_KEY")?, std::env::var("CALMLENS_PROJECT_ID")?)?;
    let bytes = std::fs::read("photo.jpg")?;
    let asset = client.assets().moderate(SubmitAsset {
        processing_options: Some(ProcessingOptions {
            features: Some(ProcessingFeatures {
                person_detection: Some(FeatureOptions::enabled()),
                face_detection: Some(FaceDetectionOptions { age_estimation: Some(true), ..FaceDetectionOptions::enabled() }),
                ..Default::default()
            }),
            ..Default::default()
        }),
        ..SubmitAsset::new("photo.jpg", Content::bytes(bytes, "image/jpeg"))
    }).await?;
    let faces = asset.report.and_then(|r| r.features).and_then(|f| f.face_detection).and_then(|f| f.faces);
    for face in faces.unwrap_or_default() {
        println!("{:?} {:?}", face.bounding_box, face.age_range);
    }
    Ok(())
}
```

**java**

```java
import com.calmlens.CalmLensServerClient;
import com.calmlens.Models.*;

public class Example {
  public static void main(String[] args) throws Exception {
    var client = new CalmLensServerClient(System.getenv("CALMLENS_API_KEY"), System.getenv("CALMLENS_PROJECT_ID"));
    byte[] bytes = java.nio.file.Files.readAllBytes(java.nio.file.Path.of("photo.jpg"));
    Asset asset = client.assets.moderate(new SubmitAsset("photo.jpg", Content.bytes(bytes, "image/jpeg"))
        .processingOptions(new ProcessingOptions().features(new ProcessingFeatures()
            .personDetection(new FeatureOptions(true))
            .faceDetection(new FaceDetectionOptions().enabled(true).ageEstimation(true)))));
    FaceDetectionResult faces = asset.report().features().faceDetection();
    if ("evaluated".equals(faces.status())) faces.faces().forEach(face -> System.out.println(face.box() + " " + face.ageRange()));
  }
}
```

**curl**

```sh
curl -X POST "https://api.calmlens.com/v1/projects/$CALMLENS_PROJECT_ID/assets?name=photo.jpg&contentType=image%2Fjpeg&queue=false&processingOptions=%7B%22features%22%3A%7B%22personDetection%22%3A%7B%22enabled%22%3Atrue%7D%2C%22faceDetection%22%3A%7B%22enabled%22%3Atrue%2C%22ageEstimation%22%3Atrue%7D%7D%7D"   -H "X-Api-Key: $CALMLENS_API_KEY"   -H "X-CalmLens-Upload: content"   -H "Content-Type: image/jpeg"   --data-binary @photo.jpg
```

- Every feature is opt-in. Leaving it out, or sending `enabled: false`, means it doesn't run.
- Unknown features and unknown options are rejected, so a typo fails the request instead of being ignored.
- Features apply to images, including images extracted from multimedia documents and websites, which inherit the parent's processing options. Other asset types ignore them.
- The features an asset was uploaded with are saved in `asset.processingOptions.features`. Retrying the asset runs the same features again.
- Person and face detection results are returned in `report.features` and never change the moderation decision.

## Asset metadata

Attach custom fields to your uploads. See [Asset metadata](/docs/guides/organising-assets#asset-metadata) for field definitions and examples.

## Folders

Pass a `folderId` to organise an upload. See [Folders](/docs/guides/organising-assets#folders) for creating, moving and managing a folder tree.
