Build MongoDB APIs in 30 Seconds with Air Pipe

Kav Pather - Oct 4 - - Dev Community

Are you tired of spending countless hours configuring your APIs and managing complex workflows for your MongoDB database? What if we told you that you could set up a fully functional API in just 30 seconds? In this article, well show you how Air Pipes cutting-edge platform makes API creation fast, efficient, and hassle-free.

Why Air Pipe?

Air Pipe revolutionizes API development, offering a platform where building, deploying, and managing APIs, integrations and workflows takes mere seconds.

Here's why Air Pipe is a game changer:

  • Instant Deployment : Get your MongoDB APIs running in 30 seconds flat.

  • Secure & Scalable : Built on Rust for memory and thread safety, ensuring your APIs are secure, fast, and scalable.

  • Flexible Integration : Effortlessly integrate with databases like MongoDB and more, connect to HTTP endpoints, and run scripts or commands.

  • Deploy Anywhere : Use Air Pipe's cloud platform or deploy it self-hosted on any infrastructure.

Step-by-Step Guide to Building MongoDB APIs

This tutorial will focus on the cloud platform deployment but will also work seamlessly for self hosted deployments, you can learn how to self host here https://docs.airpipe.io/docs/tutorial/setup .

Prerequisites:

Step 1: Create your Configuration

Once signed in to your Air Pipe account, head to Configurations > Add New.

Configuration Add

Step 2: Define your MongoDB Data Source

Defining your data source is as simple as naming your database , setting a driver and adding a connection string.

For production deployments we understand your connection string is sensitive, we provide encrypted variable (secret) support https://docs.airpipe.io/docs/configuration/managed-variables .

Other secret stores are also supported.

In the YAML editor paste the below example and update the connection string.



name: mongodb-find-example
description: my mongo api

global:
  databases:
    main: 
      driver: mongodb
      conn_string: |
        mongodb+srv://someUser:somePass@bob-perf.hello.mongodb.net/?retryWrites=true&w=majority&appName=my-test


Enter fullscreen mode Exit fullscreen mode

The global block defines the databases, variables, secrets and templates to be used across API interfaces and its subsequent actions.

Step 3: Creating HTTP interfaces

Its trivial to create a HTTP interface with Air Pipe, below we set a route name as mongodb/find , the method as POST and output to http.



name: mongodb-find-example
description: my mongo api

global:
  databases:
    main: 
      driver: mongodb
      conn_string: |
        mongodb+srv://someUser:somePass@bob-perf.hello.mongodb.net/?retryWrites=true&w=majority&appName=my-test

interfaces:
  mongodb/find: # this will be used as our route name
    method: POST # method set to POST so we can accept input data
    output: http # output set to HTTP


Enter fullscreen mode Exit fullscreen mode

The same concept can be used for creating webhooks and creating an entry point to any of your configurations that access databases, commands, scripts and other HTTP endpoints.

Step 4: Define your Actions

Actions are a set of instructions an interface will run when called.

  1. Select your database to connect to i.e. main

  2. Set the document operation and filter.

Well also add a mongodb/update interface while were at it.



name: mongodb-operations
description: use any mongodb operation

global:
  databases:
    main: 
      driver: mongodb
      conn_string: |
        mongodb+srv://someUser:somePass@bob-perf.hello.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0

interfaces:
  mongodb/find:
    method: POST
    output: http

    actions:
      - name: MongoQuery
        database: main
        document_operation:
          database: sample_analytics 
          collection: customers
          operation: findOne
          filter: '{"name": "a|body::name|"}'

  mongodb/update:
    method: POST
    output: http

    actions:
      - name: MongoQuery
        database: main
        document_operation:
          database: sample_analytics
          collection: customers
          operation: updateOne
          filter: '{"name": "a|body::name|"}'
          update: '{"$set": {"active": a|body::status|}}'


Enter fullscreen mode Exit fullscreen mode

For all available operations and settings view https://docs.airpipe.io/docs/configuration/interfaces/actions/inputs/databases#document_operation

Step 5: Deploy and Test

  • Set your permission as USER_WITH_PUBLIC_READ

  • Select Deploy to Staging and Deploy to Production and hit create.

Deploy

Its that easy your API is live and ready to be consumed in seconds.

Use your favorite API testing tool, or do a curl to your endpoints, lets try the mongodb/find route, make sure you update your organization uuid in the route.

You can find your organization uuid under Configurations or in your Organization Settings.

Configurations List

  • Send a HTTP POST with a JSON payload eg. {"name": "Elizabeth Ray"} to mongodb/find to find a document with a matching name.

  • Send a HTTP POST with a JSON payload eg. {"name": "Elizabeth Ray", "status": false} to mongodb/update to find a document with a matching name and update the status value accordingly.

https://api.airpipe.io/YOUR-ORGANIZATION-UUID/production/mongodb/find



curl --location 'https://api.airpipe.io/YOUR-ORGANIZATION-UUID/production/mongodb/find' \
--header 'Content-Type: application/json' \
--data '{
    "name": "Elizabeth Ray",
    "status": true
}'


Enter fullscreen mode Exit fullscreen mode

Example response:



{
"data": {
"MongoQuery": {
"time.ms": 3,
"data": {
"_id": {
"$oid": "5ca4bbcea2dd94ee58162a68"
},
"username": "fmiller",
"name": "Elizabeth Ray",
"address": "9286 Bethany Glens\nVasqueztown, CO 22939",
"birthdate": {
"$date": {
"$numberLong": "226117231000"
}
},
"email": "arroyocolton@gmail.com",
"active": true,
"accounts": [
371138,
324287,
276528,
332179,
422649,
387979
],
"tier_and_details": {
"0df078f33aa74a2e9696e0520c1a828a": {
"tier": "Bronze",
"id": "0df078f33aa74a2e9696e0520c1a828a",
"active": true,
"benefits": [
"sports tickets"
]
},
"699456451cc24f028d2aa99d7534c219": {
"tier": "Bronze",
"benefits": [
"24 hour dedicated line",
"concierge services"
],
"active": true,
"id": "699456451cc24f028d2aa99d7534c219"
}
}
}
}
}
}
Enter fullscreen mode Exit fullscreen mode




Youre done! Whats Next?

Air Pipe provides you a fast, consistent and robust development experience that you can continue extending with confidence across anything you build.

You can further add testing, and schema validation for your payloads to suit your requirements or fetch additional data if required.

See:

Performance and Reliability Built on Rust

One of the core strengths of Air Pipe is that it is built on Rust , known for its memory safety , thread safety , and high performance. Whether you're managing large-scale databases or smaller applications, Rust ensures your APIs run smoothly and securely.

Why MongoDB?

MongoDB is a popular choice for developers due to its flexibility, scalability, and ease of use. Whether you're building a startup or an enterprise-grade application, MongoDB allows you to store and retrieve your data seamlessly. Coupled with Air Pipe's rapid deployment capabilities, it becomes easier than ever to launch and manage data-driven applications.

Conclusion

Building MongoDB APIs doesn't have to be a daunting task. With Air Pipe, you can achieve in 30 seconds what traditionally takes hours or even days. The combination of performance, security, and flexibility makes Air Pipe an ideal solution for developers and businesses seeking to streamline their workflows and get to market faster.

Ready to get started? Head over to Air Pipe and build your first MongoDB API in just 30 seconds!

💡 Visit Air Pipe

💡 Find us on Discord

. .
Terabox Video Player