Request icon
InsertOne icon

Extract JSON data from an API and load it as a document to MongoDB

Call any REST API, capture the JSON response, and insert it as a document into MongoDB with Kestra. Parameterized, retryable, and fully declarative in YAML.

Categories
Data
id: load-pokemon
namespace: company.team

inputs:
  - id: pokemon
    type: STRING
    defaults: psyduck

tasks:
  - id: fetch_pokemon
    type: io.kestra.plugin.core.http.Request
    uri: https://pokeapi.co/api/v2/pokemon/{{ inputs.pokemon }}
    method: GET

  - id: load
    type: io.kestra.plugin.mongodb.InsertOne
    connection:
      uri: mongodb://host.docker.internal:27017/
    database: local
    collection: pokemon
    document: "{{ outputs.fetch_pokemon.body }}"

This flow extracts JSON data from a REST API and loads it as a document into MongoDB. It uses the PokeAPI as a concrete example, fetching a Pokemon by name and inserting the raw JSON response into a collection. The pattern solves a common ingestion need: pulling semi-structured data from an HTTP endpoint and persisting it into a document store without writing a custom script or standing up a separate ETL service. Because the Pokemon name is a runtime input, the same flow can target any record on demand.

How it works

  1. The fetch_pokemon task (io.kestra.plugin.core.http.Request) issues a GET request to https://pokeapi.co/api/v2/pokemon/{{ inputs.pokemon }}, where the pokemon input defaults to psyduck but can be overridden at execution time.
  2. The load task (io.kestra.plugin.mongodb.InsertOne) connects to MongoDB via the connection.uri property and inserts {{ outputs.fetch_pokemon.body }} into the pokemon collection of the local database.

What you get

  • A working API-to-database ingestion pipeline in two tasks.
  • Runtime parameterization through the pokemon STRING input.
  • Raw JSON persisted as a MongoDB document, ready to query.
  • A reusable template for any REST endpoint and document store.

Who it's for

  • Data engineers building lightweight ingestion pipelines.
  • Backend developers prototyping API-to-store flows.
  • Analysts who need external JSON data landed in MongoDB quickly.

Why orchestrate this with Kestra

A standalone script or a cron job can call an API and write to MongoDB, but it cannot give you event-driven execution, automatic retries on transient HTTP or database failures, execution-level lineage between the fetch and load steps, or a declarative YAML definition that lives in version control. Kestra wires the HTTP response directly into the MongoDB insert through {{ outputs.fetch_pokemon.body }}, so the data contract between tasks is explicit. MongoDB has no native scheduler or pipeline engine of its own, so Kestra fills the gap that the database cannot: orchestrating when and how data arrives, observing every run, and replaying failures.

Prerequisites

  • A running MongoDB instance reachable at the configured connection.uri (the example uses mongodb://host.docker.internal:27017/).
  • Network access to the target REST API (the PokeAPI requires no authentication).

Secrets

This flow references no secrets. The MongoDB connection is supplied inline through connection.uri. For production, move the URI into a secret and reference it with {{ secret('MONGODB_URI') }}.

Quick start

  1. Add the flow to your Kestra instance.
  2. Confirm MongoDB is reachable at the connection.uri value.
  3. Trigger an execution, optionally overriding the pokemon input.
  4. Query the pokemon collection in the local database to see the inserted document.

How to extend

  • Swap the API URI to ingest data from any other REST endpoint.
  • Replace InsertOne with a bulk insert or upsert to handle batches.
  • Add a Schedule or webhook trigger to run the flow automatically.
  • Insert a transform task to reshape or enrich the JSON before loading.
  • Promote the connection string to a secret for secure, environment-specific configuration.

Links

Share this Blueprint
See How

New to Kestra?

Use blueprints to kickstart your first workflows.