# How to run twitchio bot forever without manually adding the new token

**URL:** <https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439>\
**Category:** API\
**Created:** [March 24, 2023, 4:45pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439 "2023-03-24T16:45:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abdumhman](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@abdumhman](https://staging-discuss.dev.twitch.com/u/abdumhman)\
**Post date:** [March 24, 2023, 4:45pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/1 "2023-03-24T16:45:45Z")

</div>

I understand that if the bot access token expires we must get a new one from the refresh token and replace the token of my bot

bot = commands.Bot(  
token=token,  
client\_id=os.environ[‘CLIENT\_ID’],  
nick = “mynick”,  
prefix=“!”,  
initial\_channels=[channel]  
)

bot.run()

However I don’t understand how I can replace the token automatically when without stopping my server and manually changing the token every time the token expires.

Any help would be extremely appreciated thanks.

---

<div class="post-metadata">

**Author:** ![DevMcGee](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@DevMcGee](https://staging-discuss.dev.twitch.com/u/DevMcGee)\
**Post date:** [March 25, 2023, 5:54pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/2 "2023-03-25T17:54:29Z")

</div>

> **[Refreshing Access Tokens](https://dev.twitch.tv/docs/authentication/refresh-tokens/)**
>
> Refreshing Access Tokens

---

<div class="post-metadata">

**Author:** ![abdumhman](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@abdumhman](https://staging-discuss.dev.twitch.com/u/abdumhman)\
**Post date:** [March 25, 2023, 9:36pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/3 "2023-03-25T21:36:09Z")

</div>

Hello, I know how to use the refresh token to get a access token and run the bot however I am running a full stack web application and in my backend so would need to run my bot at all times without stopping the changing the access token myself. Is there a way for the bot to detect when the access token has expired and replace it automatically?

---

<div class="post-metadata">

**Author:** ![Dist](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/dist/32/7192_2.png) [@Dist](https://staging-discuss.dev.twitch.com/u/Dist)\
**Post date:** [March 25, 2023, 9:52pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/4 "2023-03-25T21:52:21Z")

</div>

You could just have your backend make the requests to the Refresh Token endpoint, and then store the new access and refresh token, such as in a database, a file, whatever is suitable for your use case.

Then whenever it needs to connect, such as after a disconnect or when the app first starts up, it just gets the token from where you’ve stored them.

---

<div class="post-metadata">

**Author:** ![abdumhman](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@abdumhman](https://staging-discuss.dev.twitch.com/u/abdumhman)\
**Post date:** [March 26, 2023, 1:54pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/5 "2023-03-26T13:54:10Z")

</div>

Apologies but im very new to twitchio could u give me an example of how you do this? How can I write the code for it to connect and use a new access token whenever there is a disconnection?

---

<div class="post-metadata">

**Author:** ![DevMcGee](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@DevMcGee](https://staging-discuss.dev.twitch.com/u/DevMcGee)\
**Post date:** [March 26, 2023, 10:10pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/6 "2023-03-26T22:10:19Z")

</div>

I’m not understanding what the issue is. Can you better describe your situation? Am I understanding correctly that you have a web component and a chat connection? You said you understand how to use refresh tokens and your question is about running forever without having to manually add a new token. Where is the issue?

> [@abdumhman](#):
>
> How can I write the code for it to connect and use a new access token whenever there is a disconnection?

I recommend checking the token’s expiration before reconnecting and refreshing it if it has expired.

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [March 28, 2023, 8:37pm UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/8 "2023-03-28T20:37:26Z")

</div>

Post removed, you leaked your client secret and a refresh token, pleaes revoke and regenerate both

---

<div class="post-metadata">

**Author:** ![DevMcGee](https://avatars.discourse-cdn.com/v4/letter/d/839c29/32.png) [@DevMcGee](https://staging-discuss.dev.twitch.com/u/DevMcGee)\
**Post date:** [March 29, 2023, 12:03am UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/10 "2023-03-29T00:03:16Z")

</div>

> [@abdumhman](#):
>
> I am running the bot like this:
> 
> ```auto
> while True:
> time.sleep(expires)
> *over here get another token from refresh
> ******** Now over here how do I replace the token of the bot and rerun it
> 
> ```

I’m not familiar with twitchio, but a quick glance at the documentation suggests you should consider using [Client.event\_token\_expired](https://twitchio.dev/en/latest/twitchio.html#twitchio.Client.event_token_expired) instead. It doesn’t look like there is another way to do it without hacking into twitchio’s internals but 🤷.

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [April 28, 2023, 12:04am UTC](https://staging-discuss.dev.twitch.com/t/how-to-run-twitchio-bot-forever-without-manually-adding-the-new-token/44439/11 "2023-04-28T00:04:07Z")

</div>

Closed on April 28, 2023, 12:04am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
