> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# How to change PTB httpx to aiohttp

> This tutorial guides you on how to change httpx python-telegram-bot to aiohttp.

## Introduction

* This article guides you through changing `python-telegram-bot` `httpx` to `aiohttp`. `httpx` and `aiohttp` are libraries used to make http requests.
* Before we get start, make sure you have Python and python-telegram-bot library installed on your environment. Check the python-telegram-bot instalation command below.

```bash theme={null}
pip install python-telegram-bot
```

## Changing libraries

### Installing

* First, you will need to install a library that offers an class to handle the requests maded to Telegram. Let's install `ptbcontrib`, use the command below:

```bash theme={null}
pip install git+https://github.com/python-telegram-bot/ptbcontrib.git@main
```

### Changing httpx to aiohttp

* Next, we need to import `AiohttpRequest` in the file where we will instantiate our bot client.

```python theme={null}
from ptbcontrib.aiohttp_request import AiohttpRequest
```

* This class will handle all requests to telegram instead of default `httpx`.
* `AiohttpRequest` will be used in the PTB client instance like in the example below:

<CodeGroup>
  ```python Bot theme={null}
  import asyncio
  import telegram
  from ptbcontrib.aiohttp_request import AiohttpRequest


  async def main():
      bot = telegram.Bot("TOKEN", request=AiohttpRequest(), get_updates_request=AiohttpRequest())
      async with bot:
          print(await bot.get_me())


  if __name__ == '__main__':
      asyncio.run(main())
  ```

  ```python ApplicationBuilder theme={null}
  import logging
  from telegram import Update
  from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
  from ptbcontrib.aiohttp_request import AiohttpRequest

  logging.basicConfig(
      format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
      level=logging.INFO
  )

  async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
      await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")

  if __name__ == '__main__':
      application = ApplicationBuilder().request(AiohttpRequest(connection_pool_size=256)).get_updates_request(AiohttpRequest()).token('TOKEN').build()
      
      start_handler = CommandHandler('start', start)
      application.add_handler(start_handler)
      
      application.run_polling()
  ```
</CodeGroup>

## Why do this change?

* Changing the library httpx to aiohttp brings some benefits with it.

1. **Performance**: aiohttp is faster than httpx.
2. **Mitigating Errors**: The httpx will raise too much network errors like ReadError and other NetworkErrors due to it's configurations and performance.

## Did you like this article?

* We created this content with great care to offer the best help possible.
  If this article helped you in any way, support our work! Leave your review! it helps us understand what matters most to you.

<CardGroup cols={2}>
  <Card title="Google Reviews" icon="google" href="https://g.page/r/CYZenpQcDgTzEAI/review">
    Leave your review in Google Reviews.
  </Card>

  <Card title="Trustpilot" icon="star" href="https://www.trustpilot.com/review/squarecloud.app">
    Leave your review in Trustpilot.
  </Card>
</CardGroup>
