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

DLUK44's avatar

Use a different API depending on the user

Hi,

I need to write an app that links to two different external APIs depending on who the user is. Each user will always use their home API. This will never/rarely change.

API1 returns something like...

'first_name' => 'Jamie',
'last_name' => 'Smith',
'score' => '133'

API2 will return...

'given_name' => 'Frankie',
'family_name' => 'Fields'
'rating' => '254'

So, I plan to have a column in the Users table that tells the app which API the particular user can be found in. I will then need a layer that normalises the data, so where the APIs have 'score' and 'rating' I will store this in my database as 'points'.

What is the best way to go about this? I would like to be able to do something like $user->update() to get the data from the API without having to worry about which particular API the user comes from.

Not expecting someone to write this for me but if someone could tell me what this might be called either in general or just in the world of Laravel that would greatly help me do the research myself.

Thank you

0 likes
1 reply
lukasyelle's avatar
Level 2

So my approach would be as follows:

You'd want to define an Interface that an API will implement, and then create concrete instances of that interface for each of the two types of APIs you will hit (So one interface, and two classes in your case). The interface could define an update method which would mean that any class which implements the interface must define that method. Therefore when you create the two API classes that each implement the API interface, they will each have to define a custom update method.

Or, if you have common functionality between the two API's (posting, getting, etc.) you could go the Abstract Class route, and leave the update method abstract in an overarching abstract API class. Then extend (instead of implement) that abstract class for each of the concrete APIs you need.

Then, depending on the user, you can use either of those API classes, and calling the update method will work on either of them.

Please or to participate in this conversation.