How to get connection string pooler URL from createProject @neondatabase/api-client sdk
I need some help with a use case where we create a new project for each organization customer. I'm using the createProject endpoint for this purpose. The endpoint returns the database connection URL, but it doesn't include the pooler.
Is there a way to directly get the pooler connection URL from the endpoint, or is the only option to manually modify the returned URL by appending --pooler?
1 Reply
grumpy-cyan•5mo ago
Hi, the API client is built on the Neon API. This method allows you to retrieve a pooled connection URI:
https://api-docs.neon.tech/reference/getconnectionuri
The api-client SDK supports it. I just tested it successfully using this example:
import 'dotenv/config';
import { createApiClient } from '@neondatabase/api-client';
async function getPooledConnectionUri() {
  const neonClient = createApiClient({
    apiKey: process.env.NEON_API_KEY!
  });
  try {
    const response = await neonClient.getConnectionUri({
      projectId: 'old-bread-12345678,        // Required: Your Neon project ID
      database_name: 'neondb',             // Required: Database name (default is usually 'neondb')
      role_name: 'neondb_owner',           // Required: Role name (default is usually 'neondb_owner')
      branch_id: 'br-bold-block-abc123,            // Optional: Branch ID (defaults to main branch if not specified)
      endpoint_id: 'ep-spring-brook-abc123,      // Optional: Specific endpoint ID
      pooled: true                         // Optional: Enable connection pooling
    });
    const connectionUri = response.data.uri;
    
    console.log('Pooled Connection URI:', connectionUri);
    return connectionUri;
    
  } catch (error) {
    console.error('Error getting connection URI:', error);
    throw error;
  }
}
async function main() {
  try {
    const uri = await getPooledConnectionUri();
    console.log('Successfully retrieved pooled connection URI');
  } catch (error) {
    console.error('Failed to get connection URI:', error);
  }
}
export { getPooledConnectionUri };
console.log('Script is running...');
main();
Neon
Retrieve connection URI
Retrieves a connection URI for the specified database. You can obtain a project_id by listing the projects for your Neon account. You can obtain the database_name by listing the databases for a branch. You can obtain a role_name by listing the roles for a branch.