|
| 1 | +# Replicate Python SDK API reference |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +```bash |
| 6 | +pip install replicate |
| 7 | +``` |
| 8 | + |
| 9 | +## Initialize a client |
| 10 | + |
| 11 | +Start by setting a `REPLICATE_API_TOKEN` environment variable in your environment. You can create a token at [replicate.com/account/api-tokens](https://replicate.com/account/api-tokens). |
| 12 | + |
| 13 | +Then use this code to initialize a client: |
| 14 | + |
| 15 | +```py |
| 16 | +import replicate |
| 17 | +``` |
| 18 | + |
| 19 | +That's it! You can now use the client to make API calls. |
| 20 | + |
| 21 | + |
| 22 | +If you want to explicitly pass the token when creating a client, you can do so like this: |
| 23 | + |
| 24 | + |
| 25 | +```python |
| 26 | +import os |
| 27 | +import replicate |
| 28 | + |
| 29 | +client = replicate.Replicate( |
| 30 | + bearer_token=os.environ["REPLICATE_API_TOKEN"] |
| 31 | +) |
| 32 | +``` |
| 33 | + |
| 34 | +## High-level operations |
| 35 | + |
| 36 | +### `replicate.use()` |
| 37 | + |
| 38 | +Create a reference to a model that can be used to make predictions. |
| 39 | + |
| 40 | +```python |
| 41 | +import replicate |
| 42 | + |
| 43 | +claude = replicate.use("anthropic/claude-sonnet-4") |
| 44 | + |
| 45 | +output = claude(prompt="Hello, world!") |
| 46 | +print(output) |
| 47 | + |
| 48 | +banana = replicate.use("google/nano-banana") |
| 49 | +output = banana(prompt="Make me a sandwich") |
| 50 | +print(output) |
| 51 | +``` |
| 52 | + |
| 53 | +Note: The `replicate.use()` method only returns output. If you need access to more metadata like prediction ID, status, metrics, or input values, use `replicate.predictions.create()` instead. |
| 54 | + |
| 55 | +### `replicate.run()` |
| 56 | + |
| 57 | +Run a model and wait for the output. This is a convenience method that creates a prediction and waits for it to complete. |
| 58 | + |
| 59 | +```python |
| 60 | +import replicate |
| 61 | + |
| 62 | +# Run a model and get the output directly |
| 63 | +output = replicate.run( |
| 64 | + "anthropic/claude-sonnet-4", |
| 65 | + input={"prompt": "Hello, world!"} |
| 66 | +) |
| 67 | +print(output) |
| 68 | +``` |
| 69 | + |
| 70 | +Note: The `replicate.run()` method only returns output. If you need access to more metadata like prediction ID, status, metrics, or input values, use `replicate.predictions.create()` instead. |
| 71 | + |
| 72 | + |
| 73 | +## API operations |
| 74 | + |
| 75 | +<!-- API_OPERATIONS --> |
| 76 | + |
| 77 | +## Low-level API |
| 78 | + |
| 79 | +For cases where you need to make direct API calls not covered by the SDK methods, you can use the low-level request interface: |
| 80 | + |
| 81 | +### Making custom requests |
| 82 | + |
| 83 | +```python |
| 84 | +import replicate |
| 85 | + |
| 86 | +client = replicate.Replicate() |
| 87 | + |
| 88 | +# Make a custom GET request |
| 89 | +response = client.get("/custom/endpoint") |
| 90 | + |
| 91 | +# Make a custom POST request with data |
| 92 | +response = client.post( |
| 93 | + "/custom/endpoint", |
| 94 | + json={"key": "value"} |
| 95 | +) |
| 96 | + |
| 97 | +# Make a custom request with all options |
| 98 | +response = client.request( |
| 99 | + method="PATCH", |
| 100 | + url="/custom/endpoint", |
| 101 | + json={"key": "value"}, |
| 102 | + headers={"X-Custom-Header": "value"} |
| 103 | +) |
| 104 | +``` |
| 105 | + |
| 106 | +See the [README](https://github.com/replicate/replicate-python-stainless/blob/main/README.md) for more details about response handing, error handling, pagination, async support, and more. |
0 commit comments