# How to maintain global variable?

**URL:** <https://forum.ionicframework.com/t/how-to-maintain-global-variable/110276>\
**Category:** ionic-v3\
**Created:** [October 28, 2017, 8:34am UTC](https://forum.ionicframework.com/t/how-to-maintain-global-variable/110276 "2017-10-28T08:34:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![azarzaza](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@azarzaza](https://forum.ionicframework.com/u/azarzaza)\
**Post date:** [October 28, 2017, 8:34am UTC](https://forum.ionicframework.com/t/how-to-maintain-global-variable/110276/1 "2017-10-28T08:34:25Z")

</div>

Please help me to handle global variables :frowning.  
I have developing App using rest API. My problem is when user login I have to store the API token into storage from server but my problem when I retrieve the token some time its return null value  
here I share my code for reference how to handle the global variable that can retrieve from the server

Login.ts

```auto
this.auth.loginUser(this.loginForm.value)
      .subscribe((data) =>{
        console.log('login processing....');
            if(data['api_status']==1)
            {
              this.storage.set('api_token',data['api_token']);

```

api.ts

```auto
public api_token:string;

async getToken(){
        await this.storage.get('api_token').then(data=>{
            this.api_token = data;
                   });
    }

```

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [October 28, 2017, 1:15pm UTC](https://forum.ionicframework.com/t/how-to-maintain-global-variable/110276/2 "2017-10-28T13:15:25Z")

</div>

Read the Angular docs on providers. Use a provider to talk to your API, and have your pages talk to your provider.
