# RPC (/sdks/python/reference/rpc)



`AsyncCQClient` exposes `send_command` / `send_query` and `subscribe_to_commands` / `subscribe_to_queries`. Responses reuse `CommandResponse` / `QueryResponse` wrappers.

## Commands [#commands]

### send\_command [#send_command]

```python
await client.send_command(message: CommandMessage) -> CommandResponse
```

**CommandMessage fields:**

| Field                | Type             | Required    | Description    |
| -------------------- | ---------------- | ----------- | -------------- |
| `channel`            | `str`            | Yes         | Target channel |
| `body`               | `bytes`          | Conditional | Payload        |
| `metadata`           | `str`            | Conditional | Metadata       |
| `timeout_in_seconds` | `int`            | Yes         | RPC timeout    |
| `tags`               | `dict[str, str]` | No          | Tags           |

### subscribe\_to\_commands [#subscribe_to_commands]

`subscribe_to_commands` is an async generator — iterate it with `async for`:

```python
async for command in client.subscribe_to_commands(
    subscription=CommandsSubscription(...),
    cancellation_token=token,
):
    # handle command and send response
    await client.send_response(CommandResponse(command_received=command, is_executed=True))
```

### send\_response (command) [#send_response-command]

```python
await client.send_response(response: CommandResponse) -> None
```

## Queries [#queries]

### send\_query [#send_query]

```python
await client.send_query(message: QueryMessage) -> QueryResponse
```

**QueryMessage** extends `CommandMessage` with:

| Field                  | Type  | Description |
| ---------------------- | ----- | ----------- |
| `cache_key`            | `str` | Cache key   |
| `cache_ttl_in_seconds` | `int` | TTL         |

### subscribe\_to\_queries [#subscribe_to_queries]

`subscribe_to_queries` is an async generator — iterate it with `async for`:

```python
async for query in client.subscribe_to_queries(
    subscription=QueriesSubscription(...),
    cancellation_token=token,
):
    # handle query and send response
    await client.send_response(QueryResponse(query_received=query, is_executed=True, body=b"result"))
```

## Quick Usage [#quick-usage]

```python title="rpc.py"
cmd = CommandMessage(
    channel="svc.commands",
    body=b"ping",
    timeout_in_seconds=5,
)
resp = await client.send_command(cmd)
```

## See Also [#see-also]

* [RPC Examples](/sdks/python/how-to/rpc/)
* [RPC pattern](/learn/rpc/getting-started)
* [Python SDK Getting Started](/sdks/python)
