We help IT Professionals succeed at work.

What are the pros and cons of making your api call from within your react app verses creating a separate api application?

I'm new to creating/using APIs and React, so can someone give me some pros and cons (or links to articles that detail the pros and cons) of making your api calls directly in your react app vs. creating a separate api application?
Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2019
Hey Michael,

Not entirely clear what you asking for, but here's my take on it.

Generally, there are 2 parts to an API - the Server and the Client. Generally, the server runs code and provides the API endpoints. It may be that the API communicates with a Database, or other services, so is generally written in a server-side language such as PHP. Those endpoints (urls) can then be consumed by a user or an application (the Client).

React is for front-end development, so generally this runs in the Browser and would act as the Client. If the React App needed data, such as a list of Customers, it would make a request (asynchronous often) to a given endpoint on the API server (for example GET https://api.mydomain.com/customers/) The API could return a JSON list of customers and your React App can present them to a user.
Michael SterlingWeb Applications Developer

Author

Commented:
I see now that my question didn't make as much sense as I originally thought. But to your point (and mine I think): React is going to make the API call, the API itself will be written OUTSIDE of the react application as it would have to be, right? I think I confused myself about where each application would/could live i.e.: both applications on the same server vs. on two different servers. So in the main regard, thank you for your response and in a lessor regard, never mind haha!
Most Valuable Expert 2018
Distinguished Expert 2019
Yeah - the client-side React App will consume the server-side API. They can both live on the same server if that's what you want, but they can also live on different servers ( or different domains).

Getting your head around all the different moving parts of more complex applications can be quite tricky :)
Michael SterlingWeb Applications Developer

Author

Commented:
"Getting your head around all the different moving parts of more complex applications can be quite tricky :) "

Man you ain't kiddin! Thanks again!