This document provides practical examples of API integrations in Lemline workflows. It demonstrates different approaches to connecting with REST APIs, GraphQL, OpenAPI, gRPC, and other API types, covering authentication, error handling, and data transformation.
Run the Examples
Complete the SQLite setup and run the commands from the setup directory. With the JAR, substitute java -jar lemline.jar for bin/lemline. Start the tutorial shop from the root of the Lemline source checkout:
docker compose -f examples/tutorial-services/compose.yaml up -d
The examples read their API keys and tokens from secrets. The shop accepts any value on these routes and lists the headers it received under Requests. Stop the setup worker with Ctrl+C, export demo values, and start it again:
The first email is valid for the shop: the customer is created as C-901 and a Welcome, Katherine Johnson message reaches their inbox. The workflow input schema accepts katherine@localhost, but the shop's check requires a domain with a dot: the second instance completes with {"result":{"success":false,"reason":"Invalid email address","email":"katherine@localhost"}}.
Pagination Handling
This example shows how to handle paginated API responses.
bin/lemline definition post -f pagination-handling.yaml
bin/lemline instance start examples pagination-handling
The shop returns three products per page. The loop reads pages 1 to 3 and stops when hasMore is false: expect totalProducts 7 and the seven IDs from SKU-1 to SKU-7.
Request Throttling
This example demonstrates how to implement API request throttling to respect rate limits.
The items are processed in batches of five, with a 2-second pause after each batch. The shop answers HTTP 429 once for SKU-2: that call is retried after 10 seconds and succeeds. SKU-404 gets HTTP 404, which is not retried and is recorded as a failure. Expect succeeded: 5 and failed: 1. Allow about a minute: the worker picks up retries every 10 seconds.
OpenAPI Integration Examples
OpenAPI Specification Integration
This example demonstrates how to use an OpenAPI specification for API integration.
The first instance returns {"result":{"success":true,"review":{"id":"R-3","rating":4,"title":"Good can"}}}. The second gets GraphQL errors for the unknown product and the rating: {"result":{"success":false,"errors":["Unknown product SKU-404","Rating must be from 1 to 5"]}}.
gRPC Integration Examples
Basic gRPC Service Call
This example demonstrates integrating with a gRPC service.
Lemline registers this definition; an instance faults with Unknown task type: CallGRPC. See How to describe a gRPC call for the HTTP equivalent on the shop.
gRPC with Authentication
This example demonstrates a gRPC call authenticated with OAuth2 client credentials.
Lemline has no AsyncAPI executor: the task fails with Unknown task type: CallAsyncAPI. Use emit and listen, as in How to emit and listen for events, to exchange events on Lemline's own channel.
Message Streaming with AsyncAPI
This example shows how to process a stream of messages with AsyncAPI: each sensor reading is sent to an analysis service, and an alert is published when the analysis asks for one. A failed call is retried up to three times.
This one fails the same way. Stop the shop when you are done:
docker compose -f examples/tutorial-services/compose.yaml down
API Integration Best Practices
When implementing API integrations in Lemline workflows, consider these best practices:
Authentication Management: Use secrets for managing API credentials securely
Request Validation: Validate input data before making API calls
Response Validation: Validate API responses against expected schemas
Error Handling: Implement robust error handling with appropriate retry policies
Rate Limiting: Respect API rate limits by implementing throttling or backoff strategies
Pagination: Handle paginated API responses properly
Idempotency: Design workflows to handle API call retries safely
Circuit Breaking: Implement circuit breaking for failing endpoints
Monitoring: Include appropriate logging for API interactions
Timeout Handling: Set appropriate timeouts for API calls (Lemline only enforces timeouts on listen tasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)
Conclusion
These examples demonstrate how to implement various API integration patterns in Lemline workflows. By leveraging the Open Workflow DSL's capabilities, you can build robust API integrations that handle authentication, error conditions, pagination, and other common challenges.
Whether you're working with REST APIs, GraphQL, OpenAPI specifications, gRPC services, or event-driven AsyncAPI definitions, Lemline provides a consistent approach to API integration that emphasizes reliability, security, and maintainability.
For more examples of error handling in API integrations, see the Error Handling Examples document.