I just landed a job as a junior developer at a marketing agency that works with HubSpot. My first task is to create a new theme for an auto part company, and I've been instructed to use serverless functions to get data from HubDB (HubSpot's database) for their products. The setup involves making an API call to my serverless function, which then calls the HubDB API, so I'm effectively using two API endpoints. I'm questioning why it's necessary to go through this extra step instead of directly accessing the HubDB API in my component. I've used NextJS with serverless functions for routing in the past, which felt more straightforward. Can someone help clarify the benefits of using serverless functions in this context? Am I missing something?
3 Answers
Another benefit is flexibility! Serverless functions can handle data transformation before sending it back to your UI. That way, you only send what's really needed, keeping the UI clean and minimizing unnecessary data exposure.
Think of serverless functions as an added layer of security. They help make your database access safer by hiding your credentials and letting you control exactly how your data is retrieved and filtered. This can prevent unauthorized access even if the HubDB API is public.
Serverless functions are great for protecting your database and its credentials. Instead of exposing your database access directly to the frontend, these functions act like a middleman, ensuring that the sensitive information remains secure. Plus, if you need to make changes down the line, it's easier to do so without messing up your main app.
Totally, plus it lets you put some business logic in there for validation and processing—all in one place!