The MongoDB connector allows you to connect one or more MongoDB Atlas Databases using the Data API to Grafbase.

The MongoDB connector accepts the following config:

  • name (required) — The unique name for the data source
  • url (required) — The MongoDB Atlas API URL
  • apiKey (required) — The MongoDB Atlas API Key
  • dataSource (required) — The MongoDB Atlas datasource
  • database (required) — The MongoDB Atlas database
import { config, connector, g } from '@grafbase/sdk'

const mongo = connector.MongoDB('MongoDB', {
  url: g.env('MONGODB_API_URL'),
  apiKey: g.env('MONGODB_API_KEY'),
  dataSource: g.env('MONGODB_DATASOURCE'),
  database: g.env('MONGODB_DATABASE'),


export default config({
  schema: g,

The url for Mongo must be for the Data API and start with https://, not mongodb://.

Grafbase automatically generates a GraphQL API to read and write to MongoDB Atlas using the models you define in the schema configuration:

const address = g
  .type('Address', {
    street: g.string(),
    city: g.string(),
    country: g.string(),

  .model('User', {
    name: g.string(),
    address: g.ref(address).optional(),
    metadata: g.json().optional(),

Models can also specify different collection name, and default values:

  .model('Author', {
    name: g.string(),
Was this page helpful?