# Search and read over HTTP

> Pick the operation for your goal, search code, follow a result to exact lines at the same commit and read every page.

Source: https://docs.corfedo.ai/connect/api/search-and-read/

The read operations search code, read files and look up dependencies and security findings. This page shows which one to use and how results lead to each other. The [API reference](https://docs.corfedo.ai/reference/api/) has every parameter.

## Before you begin

A credential with **Search and read code**, and **Dependencies** or **Security findings** for those reads. The examples use the [TypeScript SDK](https://docs.corfedo.ai/connect/sdks/typescript/) with an open `client`. The other SDKs have the same operations.

## Choose the operation

| Goal | Operation | TypeScript |
| --- | --- | --- |
| Find code by a description, a name, exact text or a pattern | `POST /search` | `client.search.search` |
| Read a file, whole or by lines | `POST /code/file` | `client.code.readFile` |
| List a directory, or the declarations in a file | `POST /code/tree`, `POST /code/symbols` | `client.code.listTree`, `client.code.listSymbols` |
| Find where a name is declared | `POST /symbols/lookup` | `client.symbols.lookup` |
| List the repositories the credential can read | `GET /repositories` | `client.repositories.list` |
| A repository's packages, or where a package is used | `POST /dependencies`, `POST /packages/uses` | `client.dependencies`, `client.packages` |
| Vulnerability findings and advisories | `POST /security/findings`, `GET /security/advisories/{advisoryId}` | `client.security` |

`/search` takes the query language in [Search syntax](https://docs.corfedo.ai/reference/search-syntax/). Results come most relevant first, a page at a time.

## Search and read the result

```ts
const page = await client.search.search({
  query: 'where are webhook signatures verified',
  scope: { repositories: [{ kind: 'name', name: 'acme/payments' }] },
});
const hit = page.results.find((result) => result.kind === 'file');
if (hit) {
  const file = await client.code.readFile({
    repository: hit.repository,
    path: hit.path,
    revision: hit.revision,
    lines: { start: 1, end: 40 },
  });
  if (file.status === 'found') console.log(new TextDecoder().decode(file.content));
}
```

Each result names its repository, path and the commit it read as `revision`. Pass that revision to the read, so it reads the same code the search found. Without a revision a read uses the commit the repository serves now.

`scope` limits the search to repositories by name, glob, ID or saved context ID. A filter such as `repo:` in the query does the same.

**Check:** `file.status` is `found` and the content contains the lines the search matched.

## Read the status

| Search status | Meaning |
| --- | --- |
| `complete` | Every repository in scope was searched |
| `partial` | Some repositories were not searched in time or are unavailable. `repositories` lists each one with its `state` and `failure`, for example `staleSnapshot` for a revision no longer served. `coverage` counts them. |
| `unavailable` | Nothing could be searched right now |

`unsearchedTexts` lists the query's texts that were [not searched](https://docs.corfedo.ai/reference/search-syntax/#terms) because they have only short or common words. A `type:symbol` page carries `declarationTotals`, the matching declarations in total, per repository and per language. They are exact when `complete` is true and lower bounds otherwise.

| Read status | Meaning |
| --- | --- |
| `found` | The file, directory or symbols were read |
| `notFound` | The path does not exist at that revision |
| `stale` | The read asked for a revision that is no longer served. Read again with `currentRevision`, or without a revision. |
| `unavailable` | The code cannot be read right now |

## Read every page

Pass the `continuation` from one page with the same request to get the next. The SDK does this for you:

```ts
for await (const page of client.pages.search({ query: '"RetryPolicy" lang:csharp' })) {
  for (const result of page.results) console.log(result.repository, result.path);
}
```

After 100 pages the traversal throws `PaginationLimitReached`. Pass `{ maximumPages: n }` as the second argument to change the limit.

## Related

- [HTTP API](https://docs.corfedo.ai/connect/api/) covers credentials, failures and limits.
- [Search and read with the CLI](https://docs.corfedo.ai/connect/cli/search-and-read/) offers the same reads in a terminal.
