# MomentIQ SDK Quickstart

MomentIQ supports two usage paths:

- **Moment Lab** for no-code testing in the browser.
- **API/SDK** for developers and AI agents.

The SDK hides the raw upload flow:

```text
local file -> R2 signed upload -> job creation -> polling -> result URL
```

## Node Example

```js
const { MomentIQ } = require('../sdk/momentiq');

const miq = new MomentIQ({
  apiKey: process.env.MOMENTIQ_API_KEY,
  baseUrl: process.env.MOMENTIQ_API_BASE_URL || 'https://api.momentiq.dev',
});

const clip = await miq.video.clipWindow({
  file: './podcast.webm',
  start: '00:01:00',
  end: '00:02:00',
});

console.log(clip.url);
```

## Upload And Poll Manually

```js
const upload = await miq.uploadFile('./podcast.webm');

const job = await miq.createJob('audio/detect-energy', {
  media_url: upload.media_url,
  settings: { min_segment_seconds: 2 },
});

const completed = await miq.pollJob(job.job_id);
console.log(completed.job.result);
```

By default, `run()` throws when the job fails. If you want to inspect a failed job yourself, call `pollJob(jobId)` directly:

```js
const status = await miq.pollJob(job.job_id);
if (status.job.status === 'failed') {
  console.error(status.job.error.code, status.job.error.message);
}
```

## Detect Energy

```js
const energy = await miq.audio.detectEnergy({
  file: './podcast.webm',
  threshold: 0.75,
});

console.log(energy.result.segments);
```

## Already-Hosted Media URL

```js
const clip = await miq.video.clipWindow({
  media_url: 'https://example.com/video.mp4',
  start: 60,
  end: 120,
});
```

## Raw API Shape

Create a job:

```bash
curl -X POST https://api.momentiq.dev/v1/jobs \
  -H "Authorization: Bearer $MOMENTIQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "endpoint": "audio/detect-energy",
    "media_url": "https://example.com/video.mp4",
    "settings": { "_run_on_worker": true }
  }'
```

Poll the job:

```bash
curl https://api.momentiq.dev/v1/jobs/job_123 \
  -H "Authorization: Bearer $MOMENTIQ_API_KEY"
```

## Live Beta Endpoints

Use these first:

- `video/clip-window`
- `video/clip-near`
- `audio/detect-silence`
- `audio/detect-energy`

Other endpoints may still be preview-only until their hosted worker behavior is tested.
