Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jwillz21's avatar

How to consume a 3rd party api

In my project I'm trying to take data from a 3rd party api and add that data into my database. The column names will save into columns in my database that do have different name (for instance access_tokem would be saved as token column in my database) I know how to make a url request in curl but I dont know how to consume the data into my database. (im open to other options)

my function

 public function addPlaid(Request $request) // adds a plaid user
    {
      $username = $request->input('bank_username');
      $password = $request->input('bank_password');
      $pin = $request->input('pin'); // set null
      $type = $request->input('type');
      // $authUser = Plaid::addAuthUser('plaid_test', 'plaid_good', null, 'chase');
      $authUser = Plaid::addAuthUser($username, $password, $pin, $type);
      return $authUser;
    }

json response

{
    "type": "device",
    "mfa": {
        "message": "Code sent to [email protected]"
    },
    "access_token": "test_chase"
}

I wanna save "type" and "access_token".

type will be saved in table:user column:type

and access_token in table: user_auth column: token

0 likes
4 replies
bobbybouwmann's avatar

This works like normal models right. So you can simply do

User::create([
    'type'  => $response->type,
]);

However you might want to connect this to the current logged in user, so you can do something like this

$user = auth()->user();
$user->update([
    'type' => $response->type,
]);

$user->userAuth()->update([
    'token' => $response->access_token,
]);

Note: Assuming you have a relationship between user and user_auth

Just to make it clear for you! It just using models and creating/updating those models.

jwillz21's avatar

Thank you so much @bobbybouwmann. Ill give this a shot right away....

Im using JWT tokens. would I connect that to the token by using the JWTAuth library? $user = JWTAuth::auth()->user();

Im currently re-building my api to use tokens instead of user id's and havent quite figured how to tie info to the logged user yet

jwillz21's avatar

@bobbybouwmann instead of using two tables and relating them Im just gonna save the access_token in the user table as access_token, and probably avoid the type column.

return $authUser; portion of my code returns my json response. If I add the update before that I get an error saying I cant update null data. If I add it after the return it doesn't update. (I'm assuming the code stops at the return.)

How can I add the update into the addPlaid()

bobbybouwmann's avatar

Like you said! You already have a logged in user right? Using the JWT library. You should do your call based on the logged in user and then you can insert all the data.

Please or to participate in this conversation.