Use Hooks Data

Examples of using data from hooks/steps to drive API iteration

Hooks can be used to query data and pass it to API endpoints for iteration. This pattern is powerful when you need to:

  • Fetch a list of IDs or parameters from a database

  • Use those values to make corresponding API calls

  • Coordinate data between multiple sources

Learn more: Query Hook | Store Hook | API Iteration

How Hook-Driven API Iteration Works

API endpoints can iterate over data provided via hooks using context.store:

  1. Use a query hook with into parameter to fetch records and store them in the store

  2. Reference the stored records in your API spec using context.store.variable_name

  3. The API endpoint iterates over each record, making one API call per record

  4. Each record's fields are accessible as state.record_field

The data flow:

Database Query ➡️ Hook Store ➡️ context.store ➡️ API Iteration ➡️ Target Database

Query-Driven Ticker Data Collection

Fetch ticker symbols from a database and retrieve market data for each one from an API.

Spec File (polygon.yaml)


Using Replication with Query Hook

Running with Sling: sling run -r /path/to/replication.yaml

Each record from the query (ticker + date) triggers one API call. The endpoint makes 100 API calls in this example.


Using Python

Date Range Generation with Store Hook

Use store hooks to build date ranges that drive API iteration.

Spec File (analytics_api.yaml)


Using Replication with Store Hook

The range() function generates an array of dates, stored in context.store.date_list, which the API endpoint iterates over.

Customer IDs with Record Enrichment

Query customer IDs and use them to fetch detailed customer data from an API.

Spec File (customers_api.yaml)


Using Replication

Processor Output to Hooks Integration

Use processor outputs (env.* and context.store.*) to pass aggregated data from API responses to hooks for validation, logging, or conditional logic.

Spec File (orders_api.yaml)


Using Replication with End Hooks


Using Python

This pattern is useful for:

  • Tracking metadata about API responses (record counts, date ranges, etc.)

  • Validating data quality before committing to the target

  • Logging detailed sync information

  • Conditional hook execution based on aggregated values

  • Updating audit or metadata tables with sync statistics

Setting Environment Variables for API Authentication

Use store hooks with env.* prefix to set environment variables that are available during API spec rendering. This is particularly powerful when you need to:

  • Dynamically compute authentication parameters before API calls

  • Inject values into authentication blocks before spec compilation

  • Pass computed values to dynamic endpoints

  • Use values from database queries or previous API calls in authentication

The key advantage: environment variables set via env.* hooks are available before the API spec is compiled/rendered, making them usable in authentication blocks and dynamic_endpoints blocks.

Spec File (api_with_dynamic_auth.yaml)


Using Pipeline with Environment Variable Setup

Running with Sling: sling run -p /path/to/pipeline.yaml


Replication File


Key Benefits

  1. Dynamic Authentication: Compute credentials at runtime (e.g., from database queries, secrets managers, or transformations)

  2. Pre-Compilation Configuration: Environment variables set via env.* hooks are available during API spec rendering, allowing use in:

    • Authentication blocks

    • Dynamic endpoint definitions

    • Default configurations

  3. Separation of Concerns: Keep sensitive or dynamic values out of spec files

  4. Database-Driven Configuration: Query databases to determine which endpoints to call or which credentials to use

  5. Multi-Environment Support: Dynamically configure API behavior based on deployment environment without changing spec files

Common Use Cases:

  • Rotating API keys fetched from a secrets manager

  • Environment-specific endpoints (dev/staging/prod)

  • Database-driven resource lists for dynamic endpoints

  • Computed authentication tokens based on current state

  • Multi-tenant API configurations

Last updated

Was this helpful?