Next.js
Next.js is a React framework for building web applications. It offers server-side rendering, automatic code splitting, and client-side routing which all enhance performance and developer productivity. It’s favored for its ability to create high-performance, SEO-friendly websites.
You can easily interface with APIs in Next.js via Sequin. Sequin will sync your API data to a Postgres database so you can write your integration in SQL. Changes you make to your database records will be applied to the API and your database at the same time.
To interact with tables in your Sequin sync in Next.js, you will do the following:
- Setup your Sequin Sync
- Connect to Sequin’s Postgres Proxy
- Set up API route handlers
Setup a Sequin sync
To setup a Sequin sync, Sequin will guide you through authenticating, selecting the data you want to sync, and connecting to your database. Read our getting started guide for step-by-step instructions.
Connect to Sequin’s Postgres Proxy
Sequin uses a Postgres Proxy to interface with your Sequin-synced tables. The Proxy lets Sequin capture inserts, updates, and deletes you make in your database and commit them to the API.
To use Sequin’s Postgres Proxy in your Next.js app, you can treat it as a regular Postgres database.
The following example creates a connection to the Proxy using the pg client:
Add .env.local to your project to prevent exposing sensitive environment variables and configuration details:
Setup your API route handlers
Next.js Route Handlers allow you to create custom request handlers for a given route using the Web Request and Response APIs.
In this setup, a Next.js frontend interacts with a backend server. The backend connects to a PostgreSQL database using pg
and exposes API routes. The frontend communicates with these routes to execute database queries and receive responses.
The following is an example of a route handler in Next.js:
The api directory structure defines the API endpoint. The example above is located in the file ./api/salesforce/products/select/route.js
and therefore results in the following endpoint:
Reading Data
With your API data in Postgres, your route handler functions can just use SQL to interface with your data.
Here’s an example function that queries for all Salesforce products in an organization:
With the route handler defined in the backend, you call it on the frontend to retrieve data. The following function sends a request to api/salesforce/products/select
:
The following is an example of a server component in Next.js, which utilizes this function to fetch the data and subsequently passes it as a prop to a child component:
Writing Data
With Sequin, you can also make mutations via your database as well. Inserts, updates, and deletes you make to Sequin-synced tables are first applied to the API. If they pass validation, they’re committed to your database.
Here’s an example of a route handler function that inserts a new record into a Salesforce products table:
This example reads from your database right after the insert, returning the fresh list of new Salesforce products. The data you just wrote will be available right away.
You can call backend API endpoint from the frontend like so:
Error handling
If a mutation fails API validation, Sequin returns the API error as a standard Postgres error.
In the previous example, we demonstrated how you can write to Postgres in a try/catch block so you can catch errors:
Let’s say you make a mutation to a Salesforce Contact with an invalid email. Salesforce will return a validation error. Your Postgres query will fail. So, the error inside your catch block will look like this:
You can pass this error down to the frontend and display a helpful message to the user.
Conclusion
Sequin transforms APIs like Salesforce into the proper database you always wanted them to be. Using SQL you can query all your third-party API data natively. No pagination, rate limits, or funky syntax. Plus, because your data is in Postgres, you have access to a plethora of tools and libraries to work with your data.