A Backend-For-Frontend Pattern
Test the service
·
Report Bug
·
Request Feature
Table of Contents
The BFF (Backend-For-Frontend) service objective is to deliver a multiple AI-generated response from different AI platforms in a single run. The client need not want to make multiple API requests for different AI services for the same prompt. Making a single call with appropriate queries to this BFF layer will do the thing. To highlight one use case, if users need to cross-verify their answers on multiple AI platforms, they need to open various AI sites and repeat the same prompt on each tab, which is a time-consuming process. To avoid such use cases, we can consume this BFF service and integrate it with any mode of platform. I implemented this service with ExpressJS on top of NodeJS, ExpressJS is a popular NodeJS framework for building efficient BFF layers. Its modular architecture, middleware capabilities, and strong community support make it an ideal choice for creating scalable and maintainable BFFs. The data access layer is handled by GraphQL, which helps to receive the specific data by querying, Unlike traditional REST APIs that often return fixed, large JSON payloads (including unnecessary fields), GraphQL allows the frontend to request exactly the data it needs, minimizing payload size. Hosted the service in Render cloud and it's live now! MIT licenses this project. If you like this project, please give it a star and follow me.
To safeguard your API key and prevent unauthorized access, our project offers a secure encryption mechanism. Simply encrypt your API key using the below steps, and pass the encrypted value to our service. We'll handle the decryption process internally, ensuring your API key remains confidential.
- Send the one-time request to get the server's public key (https://bff-expressjs-graphql.onrender.com/getPublicKey), reference;
- Encrypt your API key with the help of a public key which is generated from the crypto library in Node.js and provides cryptographic functionality for secure communications with asymmetric (public/private key pairs) methods.
Javascript:
// to do a client-side encryption
function encryptData(apiKey) {
return crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_PADDING
},
Buffer.from(apiKey, 'utf8')
).toString('base64');
}
- While passing the encrypted API key, isEncrypted query value should be true, else false.
Open the postman tool and import the below curl to get started.
curl --location --request GET 'https://bff-expressjs-graphql.onrender.com/' \
--header 'Content-Type: application/json' \
--data '{
"query": "query($geminiPayload: Payload!) { gemini(payload: $geminiPayload) }",
"variables": {
"geminiPayload": {
"apiKey": "Provide your encrypted or non-encrypted apikey here",
"model": "Provide your model here (eg: gemini-1.5-flash)",
"prompt": "Prompts go here",
"isEncrypted": false
}
}
}'
Note: gemini, chatgpt, claude - use these keywords in the query to access the appropriate AI models, reference.
typedef:
This file defines the GraphQL schema using Schema Definition Language (SDL). It describes the structure of your GraphQL API, including types, queries, mutations, and any custom scalars.
resolvers:
This file contains resolver functions that handle the logic for fetching or modifying data in response to GraphQL queries or mutations. Resolvers are responsible for interacting with data sources or services
app.js:
This file is the main entry point of your application where you set up the GraphQL server, integrate it with your web framework (like Express), and start the server
schema:
This file serves as a representation of the GraphQL schema in a format that can be used for documentation, introspection, or schema validation purposes.
View the outcomes.
Distributed under the MIT License. See LICENSE for more information.
OS: Ubuntu 24.04 LTS
Model: Customized PC
Processor: Intel i5 13th gen
Ram: DDR5 16gb
Disk: NVMe 100gb
Akash.A,
Computer Science Engineer,
akashcse2000@gmail.com,
8608550403,
Chennai.
Follow me on

