|
| 1 | +# Diffio JS SDK |
| 2 | + |
| 3 | +The Diffio JS SDK helps you call the Diffio API from Node. This version covers project creation, upload, generation, progress checks, and download URLs. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install diffio-js |
| 9 | +``` |
| 10 | + |
| 11 | +For local development: |
| 12 | + |
| 13 | +```bash |
| 14 | +cd diffio-js |
| 15 | +npm install |
| 16 | +``` |
| 17 | + |
| 18 | +## Configuration |
| 19 | + |
| 20 | +Set the API key with `DIFFIO_API_KEY`. You can also override the base URL with `DIFFIO_API_BASE_URL`. |
| 21 | + |
| 22 | +```bash |
| 23 | +export DIFFIO_API_KEY="diffio_live_..." |
| 24 | +export DIFFIO_API_BASE_URL="https://us-central1-diffioai.cloudfunctions.net" |
| 25 | +``` |
| 26 | + |
| 27 | +For emulators, set the base URL to the Functions emulator host. |
| 28 | + |
| 29 | +```bash |
| 30 | +export DIFFIO_API_BASE_URL="http://127.0.0.1:5001/diffioai/us-central1" |
| 31 | +``` |
| 32 | + |
| 33 | +## Request options |
| 34 | + |
| 35 | +Use request options to override headers, timeouts, retries, or the API key per request. |
| 36 | + |
| 37 | +```ts |
| 38 | +import { DiffioClient } from "diffio-js"; |
| 39 | + |
| 40 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 41 | +const projects = await client.listProjects({ |
| 42 | + requestOptions: { |
| 43 | + headers: { "X-Debug": "1" }, |
| 44 | + timeoutInSeconds: 30, |
| 45 | + maxRetries: 2, |
| 46 | + retryBackoff: 0.5 |
| 47 | + } |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +## Create a project and generation |
| 52 | + |
| 53 | +`createProject` uploads the file and returns the project metadata. |
| 54 | + |
| 55 | +```ts |
| 56 | +import { DiffioClient } from "diffio-js"; |
| 57 | + |
| 58 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 59 | +const filePath = "sample.wav"; |
| 60 | + |
| 61 | +const project = await client.createProject({ |
| 62 | + filePath |
| 63 | +}); |
| 64 | + |
| 65 | +const generation = await client.createGeneration({ |
| 66 | + apiProjectId: project.apiProjectId, |
| 67 | + model: "diffio-2", |
| 68 | + sampling: { steps: 12, guidance: 1.5 } |
| 69 | +}); |
| 70 | + |
| 71 | +console.log(generation.generationId); |
| 72 | +``` |
| 73 | + |
| 74 | +## Audio isolation helper |
| 75 | + |
| 76 | +```ts |
| 77 | +import { DiffioClient } from "diffio-js"; |
| 78 | + |
| 79 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 80 | +const result = await client.audioIsolation.isolate({ |
| 81 | + filePath: "sample.wav", |
| 82 | + model: "diffio-2", |
| 83 | + sampling: { steps: 12, guidance: 1.5 } |
| 84 | +}); |
| 85 | + |
| 86 | +console.log(result.generation.generationId); |
| 87 | +``` |
| 88 | + |
| 89 | +## Restore audio in one call |
| 90 | + |
| 91 | +This helper runs the full flow and returns the downloaded bytes plus a metadata object. |
| 92 | + |
| 93 | +```ts |
| 94 | +import fs from "node:fs"; |
| 95 | +import { DiffioClient } from "diffio-js"; |
| 96 | + |
| 97 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 98 | +const [audioBytes, info] = await client.restoreAudio({ |
| 99 | + filePath: "sample.wav", |
| 100 | + model: "diffio-2", |
| 101 | + sampling: { steps: 12, guidance: 1.5 }, |
| 102 | + onProgress: (progress) => console.log(progress.status) |
| 103 | +}); |
| 104 | + |
| 105 | +if (info.error) { |
| 106 | + console.log(info.error); |
| 107 | +} else if (audioBytes) { |
| 108 | + fs.writeFileSync("restored.mp3", Buffer.from(audioBytes)); |
| 109 | +} |
| 110 | + |
| 111 | +console.log(info.apiProjectId, info.generationId); |
| 112 | +``` |
| 113 | + |
| 114 | +## Generation progress |
| 115 | + |
| 116 | +```ts |
| 117 | +import { DiffioClient } from "diffio-js"; |
| 118 | + |
| 119 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 120 | +const progress = await client.generations.getProgress({ |
| 121 | + generationId: "gen_123", |
| 122 | + apiProjectId: "proj_123" |
| 123 | +}); |
| 124 | + |
| 125 | +console.log(progress.status); |
| 126 | +``` |
| 127 | + |
| 128 | +## Generation download |
| 129 | + |
| 130 | +```ts |
| 131 | +import { DiffioClient } from "diffio-js"; |
| 132 | + |
| 133 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 134 | +const download = await client.generations.getDownload({ |
| 135 | + generationId: "gen_123", |
| 136 | + apiProjectId: "proj_123", |
| 137 | + downloadType: "audio" |
| 138 | +}); |
| 139 | + |
| 140 | +console.log(download.downloadUrl); |
| 141 | +``` |
| 142 | + |
| 143 | +## List projects |
| 144 | + |
| 145 | +```ts |
| 146 | +import { DiffioClient } from "diffio-js"; |
| 147 | + |
| 148 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 149 | +const projects = await client.projects.list(); |
| 150 | + |
| 151 | +for (const project of projects.projects) { |
| 152 | + console.log(project.apiProjectId, project.status); |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## List project generations |
| 157 | + |
| 158 | +```ts |
| 159 | +import { DiffioClient } from "diffio-js"; |
| 160 | + |
| 161 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 162 | +const generations = await client.projects.listGenerations({ apiProjectId: "proj_123" }); |
| 163 | + |
| 164 | +for (const generation of generations.generations) { |
| 165 | + console.log(generation.generationId, generation.status); |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## Webhooks portal access |
| 170 | + |
| 171 | +```ts |
| 172 | +import { DiffioClient } from "diffio-js"; |
| 173 | + |
| 174 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 175 | +const portal = await client.webhooks.getPortalAccess({ mode: "test" }); |
| 176 | +console.log(portal.portalUrl); |
| 177 | +``` |
| 178 | + |
| 179 | +## Send a test webhook event |
| 180 | + |
| 181 | +```ts |
| 182 | +import { DiffioClient } from "diffio-js"; |
| 183 | + |
| 184 | +const client = new DiffioClient({ apiKey: "diffio_live_..." }); |
| 185 | +const event = await client.webhooks.sendTestEvent({ |
| 186 | + eventType: "generation.completed", |
| 187 | + mode: "test", |
| 188 | + samplePayload: { apiProjectId: "proj_123" } |
| 189 | +}); |
| 190 | + |
| 191 | +console.log(event.svixMessageId); |
| 192 | +``` |
| 193 | + |
| 194 | +## Runtime compatibility |
| 195 | + |
| 196 | +Use Node 18 or later so `fetch` is available without extra packages. |
| 197 | +Examples use ES modules. Save files with a `.mjs` extension or set `"type": "module"` in your package.json. |
| 198 | + |
| 199 | +## Tests |
| 200 | + |
| 201 | +```bash |
| 202 | +cd diffio-js |
| 203 | +npm run build |
| 204 | +``` |
0 commit comments