{"id":21539500,"url":"https://github.com/zebreus/replicate-api","last_synced_at":"2025-04-10T03:26:31.006Z","repository":{"id":61074556,"uuid":"547184780","full_name":"zebreus/replicate-api","owner":"zebreus","description":"A TypeScript client library for the replicate.com API","archived":false,"fork":false,"pushed_at":"2024-06-10T15:33:47.000Z","size":449,"stargazers_count":25,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-07T18:11:37.331Z","etag":null,"topics":["ai","api-client","javascript","replicate","stable-diffusion","typescript"],"latest_commit_sha":null,"homepage":"https://github.com/Zebreus/replicate-api","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zebreus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-07T09:13:34.000Z","updated_at":"2025-02-25T00:50:20.000Z","dependencies_parsed_at":"2023-02-19T17:46:06.873Z","dependency_job_id":null,"html_url":"https://github.com/zebreus/replicate-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebreus%2Freplicate-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebreus%2Freplicate-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebreus%2Freplicate-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebreus%2Freplicate-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zebreus","download_url":"https://codeload.github.com/zebreus/replicate-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247704571,"owners_count":20982298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","api-client","javascript","replicate","stable-diffusion","typescript"],"created_at":"2024-11-24T04:15:35.427Z","updated_at":"2025-04-10T03:26:30.965Z","avatar_url":"https://github.com/zebreus.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# replicate-api\n\nA typed client library for the [replicate.com](https://replicate.com/) API.\n\nYou can use this to access the prediction API in a type-safe and convenient way.\n\n## Install\n\nJust install it with your favorite package manager:\n\n```bash\nyarn add replicate-api\npnpm add replicate-api\nnpm install replicate-api\n```\n\nThe package should work in the browser and in Node.js [versions 18 and up](#older-node-versions).\n\n## Obtain an API token\n\nYou need an API token for nearly all operations. You can find the token in your\n[account settings](https://replicate.com/account).\n\n## Examples\n\n### Generate an image with stable-diffusion\n\nYou can create a new prediction using the\n[`stability-ai/stable-diffusion`](https://replicate.com/stability-ai/stable-diffusion) model and wait for the result\nwith:\n\n```typescript\nconst prediction = await predict({\n  model: \"stability-ai/stable-diffusion\", // The model name\n  input: { prompt: \"multicolor hyperspace\" }, // The model specific input\n  token: \"...\", // You need a token from replicate.com\n  poll: true, // Wait for the model to finish\n})\n\nconsole.log(prediction.output[0])\n// https://replicate.com/api/models/stability-ai/stable-diffusion/files/58a1dcfc-3d5d-4297-bac2-5395294fe463/out-0.png\n```\n\nThis does some things for you like resolving the model name to a model version and polling until the prediction is\ncompleted.\n\n### Create a new prediction\n\n```typescript\nconst result = await predict({ model: \"replicate/hello-world\", input: { prompt: \"...\" }, token: \"...\" })\n```\n\nThen you can check `result.status` to see if it's `\"starting\"`, `\"processing\"` or `succeeded`. If it's `\"succeeded\"` you\ncan get the outputs with `result.outputs`. If not you can check back later with `getPrediction()` and the id from\n`result` (`result.id`).\n\nYou can also set `poll: true` in the options of `predict()` to wait until it has finished. If you don't do that, you can\nstill use `.poll()` to poll until the prediction is done.\n\n### Wait until a prediction is finished\n\n```typescript\n// If you have a PredictionState:\nconst finishedPrediction = prediction.poll()\n\n// If you only have the prediction ID:\nconst finishedPrediction = await pollPrediction({ id, token: \"...\" })\n\n// If you are creating a new prediction anyways:\nconst finishedPrediction = await predict({ ...otherOptions, poll: true })\n```\n\n### Retrieve the current state of a prediction\n\n```typescript\n// If you have a PredictionState:\nconst currentPrediction = prediction.get()\n\n// If you only have the prediction ID:\nconst currentPrediction = await getPrediction({ id, token: \"...\" })\n```\n\n### Cancel a running prediction\n\n```typescript\n// If you have a PredictionState:\nconst currentPrediction = result.cancel()\n\n// If you only have the prediction ID:\nconst currentPrediction = await cancelPrediction({ id, token: \"...\" })\n```\n\nCanceling the prediction also returns the state of the prediction after canceling.\n\n### Get information about a model\n\n```typescript\nconst info = await getModel({ model: \"replicate/hello-world\", token: \"...\" })\n```\n\n### Get a list of all versions of a model\n\n```typescript\nconst info = await listVersions({ model: \"replicate/hello-world\", token: \"...\" })\n```\n\n### Generate a prediction without using the convenience functions\n\nThe first example used a few convenience functions to make it easier to use the API. You can also use the lower-level\nfunctions that map the API calls more directly.\n\n```typescript\nconst model = await getModel({ model: \"stability-ai/stable-diffusion\", token: \"...\" })\n\nlet prediction = await predict({\n  version: model.version,\n  input: { prompt: \"multicolor hyperspace\" },\n  token: \"...\",\n})\n\n// pollPrediction does this a bit smarter, with increasing backoff\nwhile (prediction.status === \"starting\" || prediction.status === \"processing\") {\n  await new Promise(resolve =\u003e setTimeout(resolve, 1000))\n  prediction = await getPrediction({ id: prediction.id, token: \"...\" })\n}\n\nconsole.log(prediction.outputs[0])\n// https://replicate.com/api/models/stability-ai/stable-diffusion/files/58a1dcfc-3d5d-4297-bac2-5395294fe463/out-0.png\n```\n\n### List your past predictions\n\n```typescript\nconst result = await listPredictions({\n  token: \"...\",\n})\n```\n\nReturns up to 100 predictions. To get more, use the `next` function:\n\n```typescript\nconst moreResults = await result.next()\n```\n\nYou can also set `all: true` to get all predictions.\n\n### Use files in your inputs\n\nTo use file inputs you need to pass them as URLs. You can use the `loadFile` function to convert local files to base64\ndata URLs:\n\n```typescript\nconst testaudioURL = await loadFile(\"./testaudio.mp3\")\n//\n```\n\nYou can also use an HTTPS URL to load files from the web.\n\n### Transcribe audio with whisper\n\nYou can create a new prediction for the [`openai/whisper`](https://replicate.com/openai/whisper) model and wait for the\nresult with:\n\n```typescript\nconst prediction = await predict({\n  model: \"openai/whisper\", // The model name\n  input: {\n    audio: await loadFile(\"./testaudio.mp3\"), // Load local file as base64 dataurl\n    // audio: \"https://raw.githubusercontent.com/zebreus/replicate-api/master/testaudio.mp3\", // Load from a URL\n    model: \"base\",\n  }, // The model specific input\n  token: \"...\", // You need a token from replicate.com\n  poll: true, // Wait for the model to finish\n})\n\nconsole.log(prediction.output.transcription)\n// Transcribed text\n```\n\n## Related projects\n\n- [replicate-js](https://github.com/nicholascelestin/replicate-js) - A js object-oriented client for replicate\n\n## Older node versions\n\nThis package uses the `fetch` API which is only supported in Node.js 18 and up. If you need to use an older version of\nNode.js, you can use `node-fetch`. It will be detected and used automatically if your node does not provide a native\nfetch. The Options object supports passing a custom fetch function, you can also try to pass `node-fetch` there.\n\n## Building and testing this package\n\nTo run the tests for this package you need an API token from \u003creplicate.com\u003e. Then you create a `src/tests/token.ts`\nfile that exports the token as a string like `export const token = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"`. Now you\ncan run `yarn test` to run the tests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzebreus%2Freplicate-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzebreus%2Freplicate-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzebreus%2Freplicate-api/lists"}