> For the complete documentation index, see [llms.txt](https://docs.nextblock.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nextblock.io/api/examples/python.md).

# Python

Complete Python examples for integrating with NextBlock's gRPC API and QUIC transaction submission using grpcio and solders/solana-py.

## Overview

These examples demonstrate how to:

* Establish secure gRPC connections with authentication
* Submit raw transaction bytes over QUIC for lower submission overhead
* Submit single and batched transactions with proper tipping
* Stream real-time tip floor data for dynamic tip optimization
* Maintain persistent connections with keepalive mechanisms

## Prerequisites

Install the required dependencies:

```bash
pip install grpcio grpcio-tools
pip install solders solana
pip install asyncio aiogrpc
pip install base58 base64
pip install requests

# Generate Python gRPC client from nextblock-proto
# See https://github.com/nextblock-ag/nextblock-proto for instructions
```

## Examples

### Core Examples

* [Connection](https://docs.nextblock.io/api/examples/python/connection.md) - Establish gRPC connections with authentication
* [QUIC Transaction Submission](https://docs.nextblock.io/api/examples/python/quic.md) - Send raw signed transaction bytes over QUIC
* [Submit Single Transaction](https://docs.nextblock.io/api/examples/python/submit-single-transactions.md) - Send individual transactions with tips
* [Submit Batched Transactions](https://docs.nextblock.io/api/examples/python/submit-batched-transactions.md) - Send atomic transaction bundles
* [Tip Floor Stream](https://docs.nextblock.io/api/examples/python/tip-floor-stream.md) - Stream real-time tip floor data
* [Keepalive](https://docs.nextblock.io/api/examples/python/keepalive.md) - Maintain persistent connections

## Quick Start

1. **Generate gRPC client** from [nextblock-proto](https://github.com/nextblock-ag/nextblock-proto)
2. **Install dependencies** using pip
3. **Set up environment** with API key and endpoint
4. **Start with connection example** to establish authenticated gRPC connection
5. **Use tip floor streaming** to optimize transaction tips dynamically
6. **Submit transactions** using single or batched submission methods

## Key Features

### Async/Await Support

All examples use Python's asyncio for non-blocking operations and better performance.

### Type Hints

Complete type annotations for better code clarity and IDE support.

### Error Handling

Comprehensive error handling with retry logic and exponential backoff.

### Connection Management

Persistent connections with keepalive, health monitoring, and automatic recovery.

### Dynamic Tipping

Real-time tip optimization based on current network conditions from tip floor API.

## Best Practices

1. **Use async/await** - Leverage Python's asyncio for better performance
2. **Enable TLS** - Always use secure connections in production
3. **Implement keepalive** - Maintain persistent connections
4. **Monitor tip floors** - Use streaming API for dynamic tip adjustment
5. **Handle errors gracefully** - Implement proper retry logic
6. **Use type hints** - Improve code clarity and maintainability
7. **Choose appropriate endpoints** - Use the closest endpoint for better latency


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.nextblock.io/api/examples/python.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
