# How to save the $auth for each request?

**URL:** <https://forum.mautic.org/t/how-to-save-the-auth-for-each-request/7256>\
**Category:** Development\
**Created:** [January 22, 2017, 6:30pm UTC](https://forum.mautic.org/t/how-to-save-the-auth-for-each-request/7256 "2017-01-22T18:30:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![italoborges](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@italoborges](https://forum.mautic.org/u/italoborges)\
**Post date:** [January 22, 2017, 6:30pm UTC](https://forum.mautic.org/t/how-to-save-the-auth-for-each-request/7256/1 "2017-01-22T18:30:39Z")

</div>

Hello! I’m new in Mautic and I’m having some trouble trying to understand how to do the requests on the API.  
  
  
  
When the user enter in my page, the authentication begins and I receive the access\_token normally.  
  
Later, I created another file( invitation.php) which is responsible for create a new contact inside Mautic.  
  
  
  
This invitation, I call with AJAX from JS.

Code:

\<?php

```
include_once __DIR__. '/mauticapi/vendor/autoload.php';
use MauticMauticApi;

$data = array(
    'firstname' => '',
    'lastname' => '',
    'email' => $_POST['email'],
    'ipAddress' => $_SERVER['REMOTE_ADDR']
);

$api = new MauticApi();
$contactApi = $api->newApi('contacts', $auth, $apiUrl);
$contact = $contactApi->create($data);

```

?\>

  
The problem is that I loose the reference to $auth.  
Could someone explain me the better way to do this?  
  
Thanks so much!

---

<div class="post-metadata">

**Author:** ![italoborges](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@italoborges](https://forum.mautic.org/u/italoborges)\
**Post date:** [January 22, 2017, 6:30pm UTC](https://forum.mautic.org/t/how-to-save-the-auth-for-each-request/7256/2 "2017-01-22T18:30:39Z")

</div>

Hello! I’m new in Mautic and I’m having some trouble trying to understand how to do the requests on the API.

When the user enter in my page, the authentication begins and I receive the access\_token normally.  
Later, I created another file( invitation.php) which is responsible for create a new contact inside Mautic.

This invitation, I call with AJAX from JS.

[code]\<?php

```
include_once __DIR__. '/mauticapi/vendor/autoload.php';
use MauticMauticApi;

$data = array(
    'firstname' => '',
    'lastname' => '',
    'email' => $_POST['email'],
    'ipAddress' => $_SERVER['REMOTE_ADDR']
);

$api = new MauticApi();
$contactApi = $api->newApi('contacts', $auth, $apiUrl);
$contact = $contactApi->create($data);

```

?\>[/code]

The problem is that I loose the reference to $auth.  
Could someone explain me the better way to do this?

Thanks so much!

---

<div class="post-metadata">

**Author:** ![MarkLL](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MarkLL](https://forum.mautic.org/u/MarkLL)\
**Post date:** [January 23, 2017, 12:50pm UTC](https://forum.mautic.org/t/how-to-save-the-auth-for-each-request/7256/3 "2017-01-23T12:50:56Z")

</div>

Hi @italoborges you need to save the tokens each time because if you are using OAuth2 it will change at some point!. See the main API page [https://github.com/mautic/api-library](https://github.com/mautic/api-library) it shows you code that you need to use to check if the tokens have changed. If they have ,you need to save it for next time. The API does not provide _any_ storage for the tokens so you need to look after them.

As an alternative, you could consider using BasicAuth. This should be available in the next API release.

---

<div class="post-metadata">

**Author:** ![italoborges](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@italoborges](https://forum.mautic.org/u/italoborges)\
**Post date:** [January 24, 2017, 3:50am UTC](https://forum.mautic.org/t/how-to-save-the-auth-for-each-request/7256/4 "2017-01-24T03:50:39Z")

</div>

Thank you very much MarkLL!!! It helped a lot!  
I’m going to store the token on database.
