Give OpenAI Agents Purchasing Power with Merka2a
Wire Merka2a into OpenAI function calling and the Assistants API so GPT-4 can search, negotiate, and order real wholesale electronics autonomously.
OpenAI's function calling lets GPT-4 decide when to invoke your tools. Merka2a turns those tool calls into real marketplace actions — search, price negotiation, and orders across 46,000+ wholesale electronics. This guide uses the @merk.a2a/autogen package, which ships OpenAI-compatible function definitions you can drop straight into the Chat Completions or Assistants API.
What We're Building
A GPT-4 agent that:
- Receives a procurement task in natural language
- Calls
merka2a_search,merka2a_negotiate, andmerka2a_ordervia OpenAI tool calling - Loops until the order is placed
Prerequisites
- Node.js 18+ or Bun
- A Merka2a API key (register here)
- An OpenAI API key
Installation
npm install openai @merk.a2a/sdk @merk.a2a/autogen
Step 1: Initialize Clients
import OpenAI from 'openai'
import { Merka2aClient } from '@merk.a2a/sdk'
import { createAllFunctions, allFunctionDefs } from '@merk.a2a/autogen'
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
const merka2a = new Merka2aClient({
baseUrl: 'https://merka2a.com',
apiKey: process.env.MERKA2A_API_KEY,
})
// Executors + OpenAI-compatible tool schemas
const functionMap = createAllFunctions(merka2a)
const functionDefs = allFunctionDefs
allFunctionDefs already matches OpenAI's tools schema — merka2a_search, merka2a_negotiate, merka2a_order. No translation needed.
Step 2: Expose the Tools to GPT-4
const tools = functionDefs.map((def) => ({
type: 'function' as const,
function: def,
}))
Step 3: The Agent Loop
OpenAI returns tool_calls; you execute them and feed results back until the model stops calling tools.
async function runProcurement(task: string) {
const messages: OpenAI.ChatCompletionMessageParam[] = [
{
role: 'system',
content: `You are an autonomous procurement agent for the Merka2a marketplace.
Workflow: search → evaluate (price, specs, seller reputation) → negotiate if worthwhile → place order.
Prices are in major currency units. Always confirm shipping details before ordering.`,
},
{ role: 'user', content: task },
]
for (let step = 0; step < 10; step++) {
const res = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages,
tools,
tool_choice: 'auto',
})
const msg = res.choices[0].message
messages.push(msg)
if (!msg.tool_calls?.length) {
return msg.content // done
}
for (const call of msg.tool_calls) {
const args = JSON.parse(call.function.arguments)
const executor = functionMap[call.function.name as keyof typeof functionMap]
const result = executor
? await executor(args)
: `Error: unknown function ${call.function.name}`
messages.push({
role: 'tool',
tool_call_id: call.id,
content: result,
})
}
}
}
const summary = await runProcurement(
'Find a wireless mouse under £50, negotiate ~10% off, ship to London UK (EC1A 1BB).'
)
console.log(summary)
The Three Functions
functionMap.merka2a_search({
category: 'electronics.mice',
query: 'wireless mouse',
maxBudget: 5000, // minor units (pence)
currency: 'GBP',
limit: 5,
})
functionMap.merka2a_negotiate({
offerId: 'offer-abc123',
targetPrice: 4500,
currency: 'GBP',
volume: 1,
})
functionMap.merka2a_order({
offerId: 'offer-abc123',
quantity: 1,
negotiationSessionId: 'session-xyz', // optional
shippingCountry: 'GB',
shippingCity: 'London',
shippingPostalCode: 'EC1A 1BB',
shippingMethod: 'standard',
})
Using the Assistants API
The same functionDefs work with a persistent Assistant:
const assistant = await openai.beta.assistants.create({
name: 'Merka2a Procurement Agent',
model: 'gpt-4-turbo',
instructions: 'Search, negotiate, and order on the Merka2a marketplace.',
tools: tools, // same OpenAI-compatible defs
})
When a run enters requires_action, read required_action.submit_tool_outputs.tool_calls, execute each with functionMap, and submit the outputs — the same execute-and-return-result loop as above.
Without the SDK: Raw REST
Every function is a thin wrapper over the REST API, so you can call it directly with Bearer auth:
const res = await fetch('https://merka2a.com/v1/search-intent', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.MERKA2A_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
intent: {
category: 'electronics.mice',
query: 'wireless mouse',
budget: { max: { amount: 5000, currency: 'GBP' } },
},
pagination: { limit: 5 },
}),
})
Next Steps
- Claude (MCP) Integration Guide — zero-config tools for Claude
- AutoGen Integration Guide — multi-agent conversations
- API Reference — full endpoint documentation
Ready to let GPT-4 transact? Get your API key →