# Call Connected Apps and APIs from Automations

> Read and write data in connected apps and Custom APIs from Type.com automation code, with typed results. Type.com adds the credentials and checks access.

Canonical page: https://docs.type.com/automations/call-integrations/

Automation code has no `fetch`. A code stage calls connected apps through Type with `ctx`. Type adds the credentials and checks access on each call. Your code never sees a credential.

| You want to | Use |
| --- | --- |
| Read with an integration tool, with a typed result | `ctx.read.call(call, schema)` |
| Change data with an integration tool, with a typed result | `ctx.write.call(call, schema, { effectKey })` |
| Read from a Custom API connection | `ctx.read.request(request, schema)` |
| Change data in a Custom API connection | `ctx.write.request(request, schema, { effectKey })` |
| Call a tool that returns only text, or a sandbox tool | `ctx.tools.call(call, options)` |
| Post in the Space | `ctx.write.sendMessage(message, { effectKey })` |

## Find a tool and its schema

Each connected app has tools. To call one, you need its `service`, `name`, input schema, and connection ID (`organizationIntegrationId`). Use one of these:

- Ask Type in a thread: "Find the Linear tool that lists issues, with its schema."
- Run `type-cli integrations list --json`, then `type-cli integrations tools <service> --integration <organizationIntegrationId> --json`.
- Call `ctx.tools.find({ service, organizationIntegrationId })` in code.

Use the exact `service`, `name`, and `organizationIntegrationId` as constants in your code. If the tool has a `rawSlug`, pass it too. The tool names on this page are examples. Use the names that discovery returns for your connection.

## The tool result

Every tool call returns the standard MCP tool result:

```ts
interface ToolResult {
  content: Array<{ type: string; text?: string }>;
  structuredContent?: unknown; // typed data, if the tool returns it
  isError?: boolean; // true when the tool reports an error
}
```

`ctx.tools.call` gives you this result as it is. `ctx.read.call` and `ctx.write.call` check it and give you only the parsed `structuredContent`.

## Read with a typed result

This automation reads 20 Linear issues every hour and returns their titles:

```ts
import {
  automation,
  code,
  complete,
  recurringEventSchema,
} from "@type-dot-com/sdk/automations";

const linear = "orgint_example"; // the Linear connection of this Space
const issues = z.object({
  issues: z.array(z.looseObject({ id: z.string(), title: z.string() })),
});

export default automation({
  output: z.object({ titles: z.array(z.string()) }),
  deployment: {
    subscriptions: {
      hourly: {
        title: "List Linear issues",
        description: "Read recent Linear issues",
        source: {
          kind: "recurring",
          schedule: { kind: "interval", everySeconds: 3600 },
        },
        entry: "list",
      },
    },
  },
  stages: {
    list: code({
      input: recurringEventSchema,
      transitions: [],
      async run({ ctx }) {
        const result = await ctx.read.call(
          {
            service: "linear",
            name: "list_issues",
            organizationIntegrationId: linear,
            arguments: { first: 20 },
          },
          issues,
        );
        if (result.isErr()) return err(result.error);
        return ok(complete({ titles: result.value.issues.map((issue) => issue.title) }));
      },
    }),
  },
});
```

`ctx.read.call` fails with an error result when:

- the call fails, for example because the automation has no access,
- the tool returns `isError: true`,
- `structuredContent` is missing or does not match your schema.

Write the schema for the provider data in `structuredContent`, not for the tool result around it:

- Describe only the fields that you use.
- Use `z.looseObject()`, so new fields from the provider do not fail the run.

> **Caution:** Some tools put JSON in `content[].text` and have no `structuredContent`. `ctx.read.call` and `ctx.write.call` fail for these tools. Use `ctx.tools.call`, then parse the text yourself.

## Call a tool that returns text

These lines go inside a code stage's `run`:

```ts
const result = await ctx.tools.call({
  service: "linear",
  name: "get_issue",
  organizationIntegrationId: "orgint_example",
  arguments: { id: "ENG-123" },
});
if (result.isErr()) return err(result.error);
if (result.value.isError)
  return err(new TypeApiError("Linear returned an error", "invalid_response"));
const text = result.value.content.map((item) => item.text ?? "").join("\n");
```

Check both `result.isErr()` and `result.value.isError`. A tool can report an error inside a successful response. `TypeApiError` comes from `@type-dot-com/sdk`.

`ctx.tools.callWithSchema(call, schema, options?)` is a middle form. It returns the full tool result, with `structuredContent` parsed by your schema.

## Change data

Use `ctx.write.call` for tools that change data. It needs an `effectKey`. In this stage, the input is a Slack message event:

```ts
const created = await ctx.write.call(
  {
    service: "linear",
    name: "create_issue",
    organizationIntegrationId: "orgint_example",
    arguments: { teamId: "TEAM", title: `Deploy failed: ${input.id}` },
  },
  z.looseObject({ id: z.string() }),
  { effectKey: `linear-issue:${input.id}` },
);
if (created.isErr()) return err(created.error);
```

Make the effect key stable for the same change. Build it from the event ID or from `scheduledAt`. An effect key does not stop a write from running two times. If a write has an unknown result, check the destination before you try again. See [Write Safely](https://docs.type.com/automations/safe-writes/index.md).

To call a tool that returns only text and changes data, use `ctx.tools.call(call, { automationEffectKey })`.

## Call a Custom API

For a [Custom API connection](https://docs.type.com/connectors-and-skills/custom-apis/index.md), send HTTP requests with `ctx.read.request` and `ctx.write.request`:

```ts
const orders = await ctx.read.request(
  { organizationIntegrationId: "orgint_example", method: "GET", path: "/orders", query: { status: "open" } },
  z.object({ orders: z.array(z.looseObject({ id: z.string() })) }),
);
if (orders.isErr()) return err(orders.error);
```

- `path` is relative to the connection's base URL.
- Type adds the connection's credentials.
- The result is the parsed response body.
- The call fails if the response status is not successful, if Type cut the response because it was too large, or if the body does not match the schema.

Use `ctx.read.request` for requests that do not change data. Use `ctx.write.request` with an `effectKey` for requests that do.

## Access rules

- The automation acts as the person who created it, with the connections of its Space.
- Access in a chat thread can differ from access in an automation. A call that works in a thread can still fail in the automation.
- A tool that can change data needs full access to the connection. With read-only access, Type.com rejects the call.
- Type checks access on each call. If someone removes a connection from the Space, later calls fail.
- In fixtures, Type answers each call with the response that you give in `toolResponses` or `integrationResponses`. A test never calls the app.

## Limits

| Item | Limit |
| --- | --- |
| Requests to Type from one code stage | 128 |
| Requests in progress at the same time | 8 |
| Response | 2 MiB. Ask for smaller pages if a response is larger. |
| Request body | 1 MiB |
| One call to a sandbox or `type-api` tool | 170 seconds |

See [Limits](https://docs.type.com/automations/reference/limits/index.md).
