
Connecting API Routes to Databases and External Services
API Routes in Next.js provide a powerful way to build backend functionality directly within your Next.js application. While you can perform simple operations, the true power emerges when you connect these routes to external data sources like databases or third-party APIs. This section explores common patterns and considerations for establishing these connections securely and efficiently.
The core principle is to treat your API Routes as server-side functions. This means you can leverage Node.js modules and environment variables to manage credentials and interact with external services. For database connections, you'll typically use a database driver or an ORM (Object-Relational Mapper).
For instance, when connecting to a PostgreSQL database using pg, you'd install the package and then establish a connection. It's crucial to manage your database credentials securely using environment variables, not hardcoded directly into your API routes.
npm install pgIn your API route file (e.g., pages/api/users.js), you can then set up a connection pool. A connection pool is recommended for performance, as it reuses database connections instead of creating a new one for every request.
import { Pool } from 'pg';
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: parseInt(process.env.DB_PORT || '5432', 10),
});
export default async function handler(req, res) {
if (req.method === 'GET') {
try {
const result = await pool.query('SELECT * FROM users');
res.status(200).json(result.rows);
} catch (error) {
console.error('Error fetching users:', error);
res.status(500).json({ message: 'Error fetching users' });
}
} else {
res.setHeader('Allow', ['GET']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}To handle environment variables, you'll typically create a .env.local file in your project's root directory. Next.js automatically loads these variables.
# .env.local
DB_USER=myuser
DB_HOST=localhost
DB_NAME=mydb
DB_PASSWORD=mypassword
DB_PORT=5432