Skip to content

Commit abc0da1

Browse files
committed
docs: simplify examples by using default import replicate pattern
- Remove explicit `replicate = Replicate()` from most examples - Use `import replicate` pattern which automatically uses REPLICATE_API_TOKEN - Keep one comprehensive example showing custom client configuration with environment variables - Maintain explicit client instantiation only where showing advanced configuration - Update Quick Start, Type Hints, and Migration Guide sections accordingly This makes the examples cleaner and follows the most common usage pattern where users rely on the default environment variable configuration.
1 parent 4a35670 commit abc0da1

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

api.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ pip install replicate
99
## Quick Start
1010

1111
```python
12-
from replicate import Replicate
13-
14-
# Initialize with REPLICATE_API_TOKEN env var by default
15-
replicate = Replicate()
12+
import replicate
1613

1714
# Create a model function
1815
banana = replicate.use("google/nano-banana")
@@ -30,37 +27,45 @@ output = replicate.run(
3027

3128
## Client Initialization
3229

33-
### Synchronous Client
30+
By default, the SDK uses the `REPLICATE_API_TOKEN` environment variable:
3431

3532
```python
36-
from replicate import Replicate
33+
import replicate
3734

38-
# Using environment variable (REPLICATE_API_TOKEN)
39-
replicate = Replicate()
35+
# Uses REPLICATE_API_TOKEN from environment
36+
output = replicate.run("google/nano-banana", input={"prompt": "hello"})
37+
```
38+
39+
### Custom Client Configuration
4040

41-
# With explicit token
42-
replicate = Replicate(bearer_token="your_api_token")
41+
For advanced use cases, you can create an explicit client instance:
4342

44-
# Legacy token parameter (for compatibility)
45-
replicate = Replicate(api_token="your_api_token")
43+
```python
44+
from replicate import Replicate
45+
import os
4646

47-
# With custom configuration
47+
# Explicitly specify which environment variable to use
4848
replicate = Replicate(
49-
bearer_token="your_api_token",
49+
bearer_token=os.environ.get("MY_REPLICATE_TOKEN"),
5050
base_url="https://api.replicate.com/v1", # Optional custom base URL
5151
timeout=120.0, # Request timeout in seconds
5252
max_retries=5 # Maximum number of retries
5353
)
54+
55+
# Now use this configured client
56+
output = replicate.run("google/nano-banana", input={"prompt": "hello"})
5457
```
5558

5659
### Asynchronous Client
5760

5861
```python
5962
from replicate import AsyncReplicate
6063
import asyncio
64+
import os
6165

6266
async def main():
63-
replicate = AsyncReplicate(bearer_token="your_api_token")
67+
# Can specify token explicitly if needed
68+
replicate = AsyncReplicate(bearer_token=os.environ.get("MY_REPLICATE_TOKEN"))
6469
output = await replicate.run(
6570
"google/nano-banana",
6671
input={"prompt": "a watercolor painting"}
@@ -664,12 +669,10 @@ The SDK respects these environment variables:
664669
The SDK is fully typed with comprehensive type hints:
665670

666671
```python
667-
from replicate import Replicate
672+
import replicate
668673
from replicate.types import Prediction, PredictionStatus
669674
from replicate.pagination import SyncCursorURLPage
670675

671-
replicate: Replicate = Replicate()
672-
673676
# Type hints for responses
674677
prediction: Prediction = replicate.predictions.get(prediction_id="abc123")
675678
status: PredictionStatus = prediction.status
@@ -708,6 +711,7 @@ from replicate import AsyncReplicate
708711

709712
async def batch_process(prompts):
710713
"""Process multiple prompts in parallel."""
714+
from replicate import AsyncReplicate
711715
replicate = AsyncReplicate()
712716
tasks = [
713717
replicate.run("model:version", input={"prompt": prompt})
@@ -780,9 +784,7 @@ model = replicate.models.get("google/nano-banana")
780784

781785
**New (v1.0+):**
782786
```python
783-
from replicate import Replicate
784-
785-
replicate = Replicate()
787+
import replicate
786788

787789
# Run a model
788790
output = replicate.run(
@@ -802,6 +804,8 @@ model = replicate.models.get(
802804
For compatibility with older code:
803805

804806
```python
807+
from replicate import Replicate
808+
805809
# Old style (still supported)
806810
replicate = Replicate(api_token="your_token")
807811

0 commit comments

Comments
 (0)