Requests & Iteration

This page details how to configure HTTP requests and use the iteration feature for looping within Sling API specifications.

Request Configuration

Each endpoint defines its HTTP request details under the request key. These settings merge with and override the defaults.request configuration.

Request Properties

Property
Required
Description
Example

url

Yes

Path (relative to defaults.request.url) or full URL

"users" or "https://api.example.com/users"

method

No

HTTP method (default: "GET")

"POST", "PUT", "PATCH", "DELETE"

headers

No

HTTP headers to send

{"Content-Type": "application/json"}

parameters

No

Query parameters (or form fields for POST)

{"page": 1, "limit": 100}

payload

No

Request body for POST/PUT/PATCH

{"name": "New User"}

timeout

No

Request timeout in seconds (default: 30)

60

rate

No

Max requests per second (default: 2)

5

concurrency

No

Max concurrent requests (default: 5)

10

Example Request Configuration

request:
  state:
    base_url: https://api.example.com/v1
    user_id: 123
  
  url: '{state.base_url}/users/{state.user_id}'
  
  # HTTP Method
  method: "POST"
  
  # Headers (merged with defaults.request.headers)
  headers:
    Content-Type: "application/json"
    Authorization: "Bearer {auth.token}"
    X-Request-ID: "{uuid()}"
    
  # Query Parameters (or form parameters for POST with application/x-www-form-urlencoded)
  parameters:
    active: true
    department: "{state.department}"
    
  # Request Body (for POST/PUT/PATCH methods)
  payload:
    user:
      name: "New User"
      email: "{state.user_email}"
      
  # Request timeout (in seconds)
  timeout: 60
  
  # Rate limiting (max requests per second)
  rate: 5
  
  # Concurrency (max in-flight requests)
  concurrency: 3

📝 Note: When method is GET or DELETE, the parameters are added as URL query parameters. When method is POST, PUT, or PATCH and Content-Type is application/x-www-form-urlencoded, the parameters are sent as form fields.

💡 Tip: Use {...} expressions to make request values dynamic based on state variables, authentication details, or environment variables.

Iteration (Looping Requests)

The iterate section allows an endpoint to make multiple requests based on a list of items (e.g., IDs from a queue, date ranges).

How Iteration Works

Iteration Properties

Property
Required
Description
Example

over

Yes

Expression to get items to iterate over

"queue.user_ids" or "[1, 2, 3]"

into

Yes

State variable to store current item

"state.current_id"

concurrency

No

Max iterations to run in parallel (default: 10)

5

if

No

Condition to evaluate before starting iteration

"state.should_process == true"

Example: Basic Iteration

⚠️ Important: Each iteration gets its own state. Changes to state variables in one iteration don't affect other iterations.

Example: Date Range Iteration

This pattern is great for splitting large data extractions into daily chunks:

Example: Batch Processing with chunk()

When an API accepts multiple IDs in a single request, use chunk() to process them in batches:

Using Context Variables for Backfill Ranges

Context variables are runtime values passed from the replication configuration to the API spec. They're particularly useful for supporting both backfill and incremental modes with the same endpoint.

Key context variables:

  • context.range_start - Start of backfill range (from source_options.range)

  • context.range_end - End of backfill range (from source_options.range)

Example: Date Range with Context Support

Replication Config for Backfill:

Replication Config for Incremental:

This pattern allows a single endpoint to handle both historical backfills and ongoing incremental updates seamlessly. See Context Variables for complete details.

Request Flow Control

You can control the request flow using these features:

Feature
Location
Purpose
Example

rate

request

Limit requests per second

rate: 5

concurrency

request

Limit parallel requests

concurrency: 3

concurrency

iterate

Limit parallel iterations

concurrency: 10

if

iterate

Conditional iteration

if: "state.has_data"

timeout

request

Set request timeout

timeout: 30

💡 Tip: Balance performance and API limits:

  • Too low concurrency: slower extraction but gentler on the API

  • Too high concurrency: faster extraction but may trigger rate limits

  • iterate.concurrency controls parallelism across different IDs/items

  • request.concurrency controls parallelism across paginated requests

Sequences: Setup and Teardown

Endpoints can define optional setup and teardown sequences that run before and after the main requests. These are multi-step workflows perfect for initialization, cleanup, or complex processes.

What are Sequences?

A sequence is an ordered array of API calls. Each call in a sequence can:

  • Make HTTP requests

  • Process responses

  • Update state variables

  • Use pagination

  • Execute conditionally

Sequences are used in three places:

  1. Authentication - Custom authentication workflows

  2. Setup - Pre-execution initialization

  3. Teardown - Post-execution cleanup

Sequence Call Structure

Each call in a sequence has the same structure:

Setup Sequences

The setup sequence runs once before the endpoint's main requests/iterations begin. Perfect for:

  • Fetching configuration data

  • Initializing session data

  • Validating prerequisites

  • Loading dynamic parameters

Basic Setup Example

Multi-Step Setup Example

Teardown Sequences

The teardown sequence runs once after all main requests complete. Perfect for:

  • Closing sessions

  • Cleaning up temporary resources

  • Logging completion status

  • Archiving processed data

Basic Teardown Example

Conditional Teardown Example

Conditional Execution with if

Each call in a sequence can execute conditionally using the if field:

Common if Patterns:

Pagination in Sequences

Sequence calls support pagination, useful for multi-page setup data:

State Management in Sequences

Important behaviors:

  1. State Isolation: Setup/teardown sequences have their own state copy

  2. State Merging: Changes to state persist back to the main endpoint

  3. Headers Inherited: Request headers from the main endpoint are copied

  4. No Iteration State: Sequences don't have iteration-specific state

Complete Sequence Example

Here's a real-world example showing setup, main execution, and teardown:

Sequence vs. Main Request

Feature
Sequences (Setup/Teardown)
Main Request

When Executes

Before/after main requests

During endpoint execution

Supports Iteration

No

Yes (via iterate)

Supports Pagination

Yes (per call)

Yes

State Scope

Shared with endpoint

Per-iteration (if iterating)

Output Records

No (state only)

Yes (to destination)

Conditional Execution

Yes (via if per call)

Yes (via iterate.if)

Best Practices for Sequences

1. Use Logging for Visibility

2. Handle Errors Gracefully

3. Keep Sequences Focused

4. Use Conditional Steps Wisely

📝 Note: Setup/Teardown sequences use the same structure as authentication sequences. State changes persist to the main endpoint, making them perfect for initialization and cleanup tasks.

💡 Tip: Use log() function liberally in sequences during development to understand the execution flow and debug issues.

Last updated

Was this helpful?