# Python, LangChain and CrewAI

> Give your Python agent Yap Factory tools: search creators, run campaigns, keep deals moving and schedule posts. `yapfactory` is an API client that also turns every [API operation](https://yapfactory.com/developers/api.md) into a LangChain or CrewAI tool.

Building in TypeScript? Use the [Vercel AI SDK](https://yapfactory.com/developers/ai-sdk.md) package. Inside ChatGPT or Claude? Use the [MCP server](https://yapfactory.com/developers/mcp.md): no code.

## Install

Client:

```bash
pip install yapfactory
```

LangChain:

```bash
pip install "yapfactory[langchain]"
```

CrewAI:

```bash
pip install "yapfactory[crewai]"
```

uv:

```bash
uv add "yapfactory[langchain]"
```

Python 3.9 or later (CrewAI needs 3.10). The client needs only `httpx`.

## Get an API key

In Yap Factory, open **Settings → AI agents → Make an API key**, then set it as `YAPFACTORY_API_KEY`. The tools act as you: a brand key gets the brand tools, a creator key the creator tools.

.env:

```bash
YAPFACTORY_API_KEY=yf_live_...
```

Acting for other people, each with their own account? Get each person's token with [OAuth](https://yapfactory.com/developers/oauth.md) and pass it as `api_key`.

## Quick start

Client:

```python
from yapfactory import YapFactory

yf = YapFactory()  # reads YAPFACTORY_API_KEY

creators = yf.call("search_creators", niches=["skincare"], limit=3)
for c in creators["data"]:
    print(c["name"], c["rate"]["formatted"], c["summary"])
```

LangChain:

```python
from langchain.agents import create_agent
from yapfactory.langchain import YapFactoryToolkit

toolkit = YapFactoryToolkit(access="read")

agent = create_agent("anthropic:claude-sonnet-4-5", tools=toolkit.get_tools())
result = agent.invoke(
    {"messages": [{"role": "user", "content": "Find three UGC creators for my skincare campaign and say why each one fits."}]}
)
print(result["messages"][-1].content)
```

CrewAI:

```python
from crewai import Agent, Crew, Task
from yapfactory.crewai import yapfactory_tools

scout = Agent(
    role="UGC scout",
    goal="Find creators who fit the brand's campaign",
    backstory="You know short-form video and what makes a creator a good fit.",
    tools=yapfactory_tools(access="read"),
)
task = Task(
    description="Find three UGC creators for my skincare campaign and say why each one fits.",
    expected_output="Three creators, one line each on why.",
    agent=scout,
)
print(Crew(agents=[scout], tasks=[task]).kickoff())
```

`access="read"` gives only the tools that change nothing, which suits a first try. Arguments use the API's names (`campaignId`) or snake_case (`campaign_id`). `AsyncYapFactory` is the same client with `await`, and the LangChain tools work with `ainvoke`.

## Ask before anything reaches people

Some tools tell other people straight away: publishing a campaign, inviting creators, messages, answering invites and scheduled posts. In LangChain each one is tagged `yapfactory:reaches-people`, and `toolkit.interrupt_on()` plugs into `HumanInTheLoopMiddleware`, so the agent stops and waits for your user's yes.

```python
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.types import Command
from yapfactory.langchain import YapFactoryToolkit

toolkit = YapFactoryToolkit()

agent = create_agent(
    "anthropic:claude-sonnet-4-5",
    tools=toolkit.get_tools(),
    middleware=[HumanInTheLoopMiddleware(interrupt_on=toolkit.interrupt_on())],
    checkpointer=InMemorySaver(),
)
config = {"configurable": {"thread_id": "1"}}

result = agent.invoke(
    {"messages": [{"role": "user", "content": "Draft a UGC campaign for my app, then invite the three best-fitting creators."}]},
    config,
)
# result["__interrupt__"] lists the calls waiting for a yes. Show them to your user, then:
agent.invoke(Command(resume={"decisions": [{"type": "approve"}]}), config)
```

CrewAI has no approval step of its own, so the CrewAI tools leave those out unless you pass `approve`, a function asked before each one. A no comes back to the model as a `not_approved` error.

```python
def approve(tool_name: str, args: dict) -> bool:
    return input(f"Run {tool_name} with {args}? [y/N] ").strip().lower() == "y"

tools = yapfactory_tools(approve=approve)
```

> No tool moves money. Starting a deal (the brand's card is charged) and approving a draft (the creator is paid) return a link to finish on yapfactory.com.

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `api_key` | `YAPFACTORY_API_KEY` | An API key (`yf_live_…`) or an OAuth access token. |
| `access` | `"all"` | `"read"`: only tools that change nothing. `"write"`: also your own drafts, briefs and scheduled posts. `"all"`: also tools that reach people and the payment links. |
| `side` | both | `"brand"` or `"creator"`: only that side's tools (shared tools stay). |
| `only` | every tool | A list of tool names, such as `["search_creators", "get_creator"]`. |
| `approve` | none | Toolkits only: `approve(tool_name, args) -> bool`, asked before each tool that reaches people. |
| `base_url` | `https://yapfactory.com/api/v1` | Another Yap Factory host, for testing. |
| `timeout`, `headers` | `30`, none | Seconds per request, and extra headers. |

## Results and errors

- Each call returns the API's JSON: `{"data": …}`, and lists also carry `nextCursor` and `hasMore`. Money is `{minor, currency, formatted}`.
- When the API refuses a call, the client raises `YapFactoryError` with `code`, `message`, `status`, `request_id` and `retry_after_seconds`. The tools return the same fields as `{"error": {…}}` instead of raising, so the model can tell the person what happened. `message` is written for people. See [errors](https://yapfactory.com/developers/errors.md).
- Calls that change something send an `Idempotency-Key`. The tools make it from the model's tool call id, so a retried step never runs twice; with the client, pass `idempotency_key=`.
- [Rate limits](https://yapfactory.com/developers/pagination.md) are per key: 120 reads, 20 writes and 10 calls that reach people a minute.

## OpenAI Agents SDK

No package needed: the OpenAI Agents SDK talks to the [MCP server](https://yapfactory.com/developers/mcp.md) directly, with your API key.

```python
import asyncio, os
from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp

async def main():
    async with MCPServerStreamableHttp(
        name="Yap Factory",
        params={
            "url": "https://yapfactory.com/mcp",
            "headers": {"Authorization": f"Bearer {os.environ['YAPFACTORY_API_KEY']}"},
        },
    ) as yapfactory:
        agent = Agent(name="UGC scout", instructions="Help the brand find UGC creators.", mcp_servers=[yapfactory])
        result = await Runner.run(agent, "Find three UGC creators for my skincare campaign.")
        print(result.final_output)

asyncio.run(main())
```

## The tools

One tool per API operation, named like the operation. Each links to its reference page.

| Tool | For | Kind |
| --- | --- | --- |
| [`get_account`](https://yapfactory.com/developers/api/get_account.md) | Everyone | Reads |
| [`list_deals`](https://yapfactory.com/developers/api/list_deals.md) | Everyone | Reads |
| [`get_deal`](https://yapfactory.com/developers/api/get_deal.md) | Everyone | Reads |
| [`send_deal_message`](https://yapfactory.com/developers/api/send_deal_message.md) | Everyone | Reaches people: asks first |
| [`search_creators`](https://yapfactory.com/developers/api/search_creators.md) | Brands | Reads |
| [`get_creator`](https://yapfactory.com/developers/api/get_creator.md) | Brands | Reads |
| [`list_products`](https://yapfactory.com/developers/api/list_products.md) | Brands | Reads |
| [`list_campaigns`](https://yapfactory.com/developers/api/list_campaigns.md) | Brands | Reads |
| [`get_campaign`](https://yapfactory.com/developers/api/get_campaign.md) | Brands | Reads |
| [`create_campaign_draft`](https://yapfactory.com/developers/api/create_campaign_draft.md) | Brands | Your own things |
| [`update_campaign_brief`](https://yapfactory.com/developers/api/update_campaign_brief.md) | Brands | Your own things |
| [`publish_campaign`](https://yapfactory.com/developers/api/publish_campaign.md) | Brands | Reaches people: asks first |
| [`set_campaign_status`](https://yapfactory.com/developers/api/set_campaign_status.md) | Brands | Reaches people: asks first |
| [`invite_creators`](https://yapfactory.com/developers/api/invite_creators.md) | Brands | Reaches people: asks first |
| [`pass_on_creator`](https://yapfactory.com/developers/api/pass_on_creator.md) | Brands | Reaches people: asks first |
| [`request_revision`](https://yapfactory.com/developers/api/request_revision.md) | Brands | Reaches people: asks first |
| [`browse_campaigns`](https://yapfactory.com/developers/api/browse_campaigns.md) | Creators | Reads |
| [`view_campaign_brief`](https://yapfactory.com/developers/api/view_campaign_brief.md) | Creators | Reads |
| [`ask_to_join`](https://yapfactory.com/developers/api/ask_to_join.md) | Creators | Reaches people: asks first |
| [`respond_to_invite`](https://yapfactory.com/developers/api/respond_to_invite.md) | Creators | Reaches people: asks first |
| [`list_post_accounts`](https://yapfactory.com/developers/api/list_post_accounts.md) | Brands | Reads |
| [`get_post_account`](https://yapfactory.com/developers/api/get_post_account.md) | Brands | Reads |
| [`list_postable_videos`](https://yapfactory.com/developers/api/list_postable_videos.md) | Brands | Reads |
| [`prepare_post_upload`](https://yapfactory.com/developers/api/prepare_post_upload.md) | Brands | Your own things |
| [`schedule_post`](https://yapfactory.com/developers/api/schedule_post.md) | Brands | Reaches people: asks first |
| [`list_posts`](https://yapfactory.com/developers/api/list_posts.md) | Brands | Reads |
| [`update_post`](https://yapfactory.com/developers/api/update_post.md) | Brands | Your own things |
| [`cancel_post`](https://yapfactory.com/developers/api/cancel_post.md) | Brands | Your own things |
| [`list_webhooks`](https://yapfactory.com/developers/api/list_webhooks.md) | Everyone | Reads |
| [`get_webhook`](https://yapfactory.com/developers/api/get_webhook.md) | Everyone | Reads |
| [`create_webhook`](https://yapfactory.com/developers/api/create_webhook.md) | Everyone | Your own things |
| [`delete_webhook`](https://yapfactory.com/developers/api/delete_webhook.md) | Everyone | Your own things |
| [`list_events`](https://yapfactory.com/developers/api/list_events.md) | Everyone | Reads |
| [`start_deal`](https://yapfactory.com/developers/api/start_deal.md) | Brands | Returns a link |
| [`approve_draft`](https://yapfactory.com/developers/api/approve_draft.md) | Brands | Returns a link |

## Source and support

- PyPI: [yapfactory](https://pypi.org/project/yapfactory/). MIT licence.
- Source: [github.com/shehrum/yapfactory-plugin](https://github.com/shehrum/yapfactory-plugin/tree/main/python).
- Questions: [support@yapfactory.com](mailto:support@yapfactory.com).

---

HTML version: https://yapfactory.com/developers/python
