Skip to content

Commit c54f09f

Browse files
authored
Data API migration guide: Clarify API key and fix broken Node.js link (#880)
Clarify API key information and fix broken Node.js link
1 parent 3c99b56 commit c54f09f

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

source/data-api/migration/data-api-tutorial.txt

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Guide: Implement an Express.js Alternative to the Atlas Data API
1212
:backlinks: none
1313
:depth: 2
1414
:class: singlecol
15-
15+
1616
As of September 2024, the Atlas Data API has been deprecated. If you use this
1717
service, you must migrate to another solution before September 2025.
1818

@@ -22,10 +22,11 @@ driver-based custom API to replicate the functionality of the Data API.
2222

2323
The API template is built with Node.js and `Express <https://expressjs.com>`__,
2424
and supports deployment on `Vercel <https://vercel.com>`__.
25-
The backend service is secured with API key-based authorization and uses the
26-
`MongoDB Node.js driver <https://nodejs.org/en>`__ along with
25+
The backend service uses the
26+
`MongoDB Node.js driver <https://nodejs.org/en>`__ with
2727
`Mongoose <https://mongoosejs.com>`__ to perform CRUD (Create, Read, Update, and
28-
Delete) and aggregation operations on your data.
28+
Delete) and aggregation operations on your data, and secures access using a
29+
simple API key-based authorization.
2930

3031
The project source code is available in the following GitHub repository:
3132
`https://github.com/abhishekmongoDB/data-api-alternative
@@ -61,14 +62,14 @@ Project Structure
6162
├── package.json
6263
└── vercel.json
6364

64-
The main files are:
65+
The main files are:
6566

6667
- **.env:** The configuration file holding credentials, connection details, and project settings.
6768
- **index.js:** The main entry point for the Express server.
68-
- **routes/api.js:** Exposes the API endpoints mapped to their corresponding business logic.
69+
- **routes/api.js:** Exposes the API endpoints mapped to their corresponding business logic.
6970
- **connection/databaseManager.js:** Manages and caches MongoDB database connections.
7071
- **models/dynamicModel.js:** Dynamically generated Mongoose models that support schemaless Atlas data.
71-
- **controllers/dbController.js:** The business logic for the defined CRUD and aggregation operations.
72+
- **controllers/dbController.js:** The business logic for the defined CRUD and aggregation operations.
7273

7374
These files are described in more detail below.
7475

@@ -80,23 +81,19 @@ Prerequisites
8081

8182
- A deployed MongoDB cluster or local instance with a
8283
`valid connection string <https://www.mongodb.com/docs/manual/reference/connection-string/>`__.
83-
- A valid `Atlas API key
84-
<https://www.mongodb.com/docs/atlas/configure-api-access/#std-label-create-org-api-key>`__
85-
to authenticate requests.
86-
You can grant programmatic access at the organization or project level.
8784
- The latest stable version of `Node.js and npm (Node Package Manager) <https://nodejs.org/en>`__
8885
installed.
8986

9087
Set Up the Project
9188
~~~~~~~~~~~~~~~~~~
9289

93-
Clone the repository and install the dependencies:
90+
Clone the repository and install the dependencies:
9491

9592
.. code-block:: shell
9693

9794
git clone https://github.com/abhishekmongoDB/data-api-alternative.git
9895
cd data-api-alternative
99-
npm install
96+
npm install
10097

10198
Define Environment Variables
10299
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -109,7 +106,9 @@ following code:
109106
# Replace with your deployment credentials
110107
MONGO_URI="<MONGO_URI>"
111108
MONGO_OPTIONS="<MONGO_OPTIONS>" # Optional
112-
API_KEY="<API_KEY>"
109+
110+
# Replace with your locally defined secrets for basic proxy auth
111+
API_KEY="<API_KEY>"
113112
API_SECRET="<API_SECRET>"
114113

115114
# Project variables
@@ -118,7 +117,7 @@ following code:
118117
RATE_LIMIT_MAX=100 # Maximum requests per window
119118
RATE_LIMIT_MESSAGE=Too many requests, please try again later.
120119

121-
Replace the following placeholders with your credentials:
120+
Replace the following placeholders with your credentials:
122121

123122
- ``MONGO_URI`` Replace with the connection string for your deployment. For more information, see `Connection String Formats <https://www.mongodb.com/docs/manual/reference/connection-string/#connection-string-formats>`__.
124123

@@ -127,8 +126,8 @@ Replace the following placeholders with your credentials:
127126
``"mongodb+srv://[<user>:<pw>@]<cluster>.<projectId>.mongodb.net"``
128127

129128
- ``MONGO_OPTIONS`` Replace with the optional query string specifying any connection-specific options. For more information, see `Connection String Options <https://www.mongodb.com/docs/manual/reference/connection-string-options/>`__.
130-
- ``API_KEY`` Replace with a valid API key.
131-
- ``API_SECRET`` Replace with the corresponding API secret.
129+
- ``API_KEY`` Replace with a locally defined key used to authenticate requests to your proxy server. This is not an Atlas API key.
130+
- ``API_SECRET`` Replace with the corresponding locally defined secret.
132131

133132
Initialize the Server
134133
~~~~~~~~~~~~~~~~~~~~~
@@ -148,17 +147,18 @@ The server includes middleware for the following:
148147
:caption: index.js
149148

150149
/**
151-
* This file initializes the server, validates API keys for added security,
152-
* and routes requests to the appropriate controller methods.
150+
* This file initializes the Express server, validates locally defined API keys,
151+
* applies basic rate limiting, and routes incoming requests to API controller methods.
153152
*/
154153
const rateLimit = require("express-rate-limit");
155154
const express = require("express");
156155
const apiRoutes = require("./routes/api");
157156
const logger = require("./utils/logging");
158157
require("dotenv").config();
159158

160-
const API_KEY = process.env.API_KEY; // Load API key from .env
161-
const API_SECRET = process.env.API_SECRET; // Load API secret from .env
159+
// Load local shared secrets from environment variables
160+
const API_KEY = process.env.API_KEY;
161+
const API_SECRET = process.env.API_SECRET;
162162

163163
const app = express();
164164

@@ -174,7 +174,8 @@ The server includes middleware for the following:
174174
// Middleware for parsing requests
175175
app.use(express.json());
176176

177-
// Middleware for API key authentication and logging
177+
// Middleware for basic API key authentication
178+
// NOTE: Replace this with your preferred authentication method in production
178179
app.use((req, res, next) => {
179180
logger.info({
180181
method: req.method,
@@ -185,7 +186,7 @@ The server includes middleware for the following:
185186
const apiKey = req.headers["x-api-key"];
186187
const apiSecret = req.headers["x-api-secret"];
187188
if (apiKey === API_KEY && apiSecret === API_SECRET) {
188-
next(); // Proceed to the next middleware or route
189+
next(); // Authorized
189190
} else {
190191
res.status(403).json({ message: "Forbidden: Invalid API Key or Secret" });
191192
}
@@ -237,7 +238,7 @@ The ``databaseManager.js`` file handles the MongoDB database connections using
237238
the ``mongoose`` library. Connection details are stored in the ``.env`` file.
238239

239240
For an exhaustive list of available options, see
240-
:node-driver:`Connection Options </node/current/fundamentals/connection/connection-options/>`
241+
:node-driver:`Connection Options <node/current/fundamentals/connection/connection-options/>`
241242
in the MongoDB Node.js Driver documentation.
242243

243244
.. code-block:: javascript
@@ -742,16 +743,19 @@ by email and counts the number of occurrences:
742743
Next Steps
743744
----------
744745

745-
This guide illustrated how you might implement a custom driver-based API as an
746-
alternative to the deprecated Atlas Data API. However, the template app is
747-
limited in scope and demonstrates basic functionality only. We strongly
748-
recommend implementing additional enhancements to meet your specific
749-
requirements.
746+
This guide demonstrated how to implement a custom driver-based API as an
747+
alternative to the deprecated Atlas Data API. The provided app is limited in
748+
scope and is intended only as a template that can be adapted and extended to
749+
meet your specific needs and requirements.
750+
751+
We strongly recommend adding production-ready security and reliability
752+
features—-such as authentication, logging, error handling, and input
753+
validation—-before deploying.
750754

751-
Additional Features
755+
Additional Features
752756
~~~~~~~~~~~~~~~~~~~
753757

754-
The following are recommended features to enhance the template app:
758+
The following are recommended features to enhance the template app:
755759

756760
- **Enhanced Logging:** Implement structured logging for better observability and debugging.
757761
- **Error Tracking:** Integrate with error tracking tools to monitor API health.

0 commit comments

Comments
 (0)