> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.aiola.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.aiola.ai/_mcp/server.

# Text to Speech - Streaming

Stream real-time audio generation with our SDKs. This guide covers how to implement streaming text-to-speech synthesis for immediate audio playback and low-latency applications.

## Prerequisites

Before you begin, make sure you have:

* An aiOla API key ([get one here](https://platform.aiola.ai/))
* Python 3.10+ (for Python SDK) or Node.js 18+ (for TypeScript SDK)

## Step 1: Set up authentication

First, generate an access token and create your client:

> **Info**
>
> For comprehensive authentication details, security considerations, and token management strategies, see our [Authentication Guide](../authentication).

#### Python

```python
from aiola import AiolaClient

# Generate access token
result = AiolaClient.grant_token(api_key='your-api-key')
access_token = result.access_token

# Create client
client = AiolaClient(access_token=access_token)
```

#### TypeScript

```typescript
import { AiolaClient } from '@aiola/sdk';

// Generate access token
const { accessToken } = await AiolaClient.grantToken({
  apiKey: 'your-api-key'
});

// Create client
const client = new AiolaClient({ accessToken });
```

## Step 2: Basic streaming synthesis

Here's how to stream audio generation for immediate processing:

#### Python

```python
# Stream audio generation
text = "This is streaming text to speech synthesis."

stream = client.tts.stream(
    text=text,
    voice='tara',
    language='en'
)

# Collect audio chunks as they arrive
audio_chunks = []
for chunk in stream:
    audio_chunks.append(chunk)
    # Process chunk in real-time if needed
    print(f"Received chunk of {len(chunk)} bytes")

print("Streaming synthesis completed!")
```

#### TypeScript

```typescript
// Stream audio generation
const text = "This is streaming text to speech synthesis.";

const stream = await client.tts.stream({
  text: text,
  voice: 'tara',
  language: 'en'
});

// Collect audio chunks as they arrive
const audioChunks = [];
for await (const chunk of stream) {
  audioChunks.push(chunk);
  // Process chunk in real-time if needed
  console.log(`Received chunk of ${chunk.length} bytes`);
}

console.log("Streaming synthesis completed!");
```

## Step 3: Async streaming (Python)

For asynchronous streaming operations:

#### Python

```python
from aiola import AsyncAiolaClient
import asyncio

async def async_streaming_example():
    # Generate access token
    result = await AsyncAiolaClient.grant_token(api_key='your-api-key')
    access_token = result.access_token
    
    # Create async client
    async_client = AsyncAiolaClient(access_token=access_token)
    
    text = "This demonstrates async streaming synthesis."
    
    stream = async_client.tts.stream(
        text=text,
        voice='tara',
        language='en'
    )
    
    # Process chunks asynchronously
    audio_chunks = []
    async for chunk in stream:
        audio_chunks.append(chunk)
        print(f"Received chunk: {len(chunk)} bytes")
            
    print("Async streaming completed!")

if __name__ == "__main__":
    # Run the async function
    asyncio.run(async_streaming_example())
```

## Best practices

1. **Chunk Processing**: Process chunks immediately for lower latency
2. **Buffer Management**: Implement proper audio buffering for smooth playback
3. **Error Recovery**: Handle network issues and retry failed streams
4. **Memory Usage**: Process chunks incrementally to avoid memory buildup
5. **Audio Quality**: Use appropriate sample rates and formats for your use case

## Next steps

Now that you've implemented streaming text-to-speech synthesis, you can:

* Explore [Text to Speech SDK](quickstart) for file-based synthesis
* Learn about [Speech to Text Streaming](../speech-to-text/streaming) for real-time transcription
* Check out the SDK repositories for more examples:
  * [Python SDK](https://github.com/aiola-lab/aiola-python-sdk)
  * [TypeScript SDK](https://github.com/aiola-lab/aiola-js-sdk)