Examples
File to HTTP
Read a CSV file and send each row to an API.
// capabilities/file-to-http/route.ts
import { craft, csv, http } from '@routecraft/routecraft'
import { z } from 'zod'
const customer = z.object({
first_name: z.string(),
last_name: z.string(),
email: z.string(),
status: z.string(),
})
export default craft()
.id('file-to-http')
.input({ body: z.array(customer) })
.from(csv({ path: './customers.csv', header: true }))
.split()
.filter(ex => ex.body.status === 'active')
.transform(row => ({
name: row.first_name + ' ' + row.last_name,
email: row.email,
}))
.to(http({
url: 'https://api.example.com/users',
method: 'POST',
}))
Input data
customers.csv:
first_name,last_name,email,status
John,Doe,john@test.com,active
Jane,Smith,jane@test.com,inactive
Bob,Wilson,bob@test.com,active
What it does
- Reads
customers.csvwith headers parsed as object keys - Filters to only
activerows - Combines first and last name into a single
namefield - POSTs each transformed row to the API
Result
Two HTTP POST requests, one per active row:
{ "name": "John Doe", "email": "john@test.com" }
{ "name": "Bob Wilson", "email": "bob@test.com" }
Jane is skipped because her status is inactive.
Run it
The csv() adapter needs its optional peer, papaparse:
bun add papaparse
Replace https://api.example.com/users with an endpoint you can reach (a local mock or a request-bin service works), save the capability as capabilities/file-to-http/route.ts and customers.csv in the project root, then start the project from that folder, since the path is resolved against where the process starts:
bun run start