Trawley
Integration guides

LangChain

Wrap your Trawley scraper as a LangChain tool for an agent.

This guide wraps Trawley's hybrid search endpoint as a LangChain tool, so a LangChain agent can call it.

bash
npm install langchain @langchain/core @langchain/openai zod

Define the tool

LangChain's tool helper takes a function plus a Zod schema describing its input.

ts
import { tool } from '@langchain/core/tools'
import { z } from 'zod'

const TRAWLEY_SCRAPER_ID = 'scr_8f2a1c9e'

export const searchListings = tool(
  async ({ query }) => {
    const params = new URLSearchParams({ search: query, take: '10' })
    const res = await fetch(
      `https://api.trawley.ai/v1/scrapers/${TRAWLEY_SCRAPER_ID}/hybrid?${params}`,
    )
    const { data } = await res.json()
    // Tools must return a string; the model reads it as the observation.
    return JSON.stringify(data)
  },
  {
    name: 'search_listings',
    description:
      'Search live property listings from acmehomes.co.uk. Accepts a natural ' +
      'language query; constraints like price, bedrooms, or date are understood. ' +
      'Returns structured records.',
    schema: z.object({
      query: z.string().describe('What to find, e.g. "3 bed houses under £500k with a garden"'),
    }),
  },
)

Use it in an agent

ts
import { ChatOpenAI } from '@langchain/openai'
import { createReactAgent } from 'langchain/agents'
import { searchListings } from './search-listings'

const model = new ChatOpenAI({ model: 'gpt-4.1' })

const agent = createReactAgent({
  llm: model,
  tools: [searchListings],
})

const result = await agent.invoke({
  messages: [
    { role: 'user', content: 'Find a 3 bed house near Kendal with a garden under £500k.' },
  ],
})

console.log(result.messages.at(-1)?.content)

A LangChain tool must return a string, so serialise the records with JSON.stringify. The agent reads that string as the tool observation and uses it to answer.

What's next