# Search code

From **GitHub v3 REST API**.

`GET /search/code`

Searches for query terms inside of a file. This method returns up to 100 results [per page](https://docs.github.com/enterprise-server@3.0/rest/overview/resources-in-the-rest-api#pagination).

When searching for code, you can get text match metadata for the file **content** and file **path** fields when you pass the `text-match` media type. For more details about how to receive highlighted search results, see [Text match metadata](https://docs.github.com/enterprise-server@3.0/rest/reference/search#text-match-metadata).

For example, if you want to find the definition of the `addClass` function inside [jQuery](https://github.com/jquery/jquery) repository, your query would look something like this:

`q=addClass+in:file+language:js+repo:jquery/jquery`

This query searches for the keyword `addClass` within a file's contents. The query limits the search to files where the language is JavaScript in the `jquery/jquery` repository.

#### Considerations for code search

Due to the complexity of searching code, there are a few restrictions on how searches are performed:

*   Only the _default branch_ is considered. In most cases, this will be the `master` branch.
*   Only files smaller than 384 KB are searchable.
*   You must always include at least one search term when searching source code. For example, searching for [`language:go`](https://github.com/search?utf8=%E2%9C%93&q=language%3Ago&type=Code) is not valid, while [`amazing
language:go`](https://github.com/search?utf8=%E2%9C%93&q=amazing+language%3Ago&type=Code) is.

## Parameters

### `q`

- Location: query
- Required: true
- Type: `string`

The query contains one or more search keywords and qualifiers. Qualifiers allow you to limit your search to specific areas of GitHub. The REST API supports the same qualifiers as GitHub.com. To learn more about the format of the query, see [Constructing a search query](https://docs.github.com/enterprise-server@3.0/rest/reference/search#constructing-a-search-query). See "[Searching code](https://docs.github.com/articles/searching-code/)" for a detailed list of qualifiers.

### `sort`

- Location: query
- Required: false
- Type: `string`
- Allowed values: `"indexed"`

Sorts the results of your query. Can only be `indexed`, which indicates how recently a file has been indexed by the GitHub Enterprise Server search infrastructure. Default: [best match](https://docs.github.com/enterprise-server@3.0/rest/reference/search#ranking-search-results)

### `order`

- Location: query
- Required: false
- Type: `string`
- Default: `desc`
- Allowed values: `"desc"`, `"asc"`

Determines whether the first search result returned is the highest number of matches (`desc`) or lowest number of matches (`asc`). This parameter is ignored unless you provide `sort`.

### `per_page`

- Location: query
- Required: false
- Type: `integer`
- Default: `30`

Results per page (max 100)

### `page`

- Location: query
- Required: false
- Type: `integer`
- Default: `1`

Page number of the results to fetch.

## Responses

### `200`

Response

### `application/json`

- Type: `object`

```json
{
  "incomplete_results": false,
  "items": [
    {
      "git_url": "https://api.github.com/repositories/167174/git/blobs/d7212f9dee2dcc18f084d7df8f417b80846ded5a",
      "html_url": "https://github.com/jquery/jquery/blob/825ac3773694e0cd23ee74895fd5aeb535b27da4/src/attributes/classes.js",
      "name": "classes.js",
      "path": "src/attributes/classes.js",
      "repository": {
        "archive_url": "https://api.github.com/repos/jquery/jquery/{archive_format}{/ref}",
        "assignees_url": "https://api.github.com/repos/jquery/jquery/assignees{/user}",
        "blobs_url": "https://api.github.com/repos/jquery/jquery/git/blobs{/sha}",
        "branches_url": "https://api.github.com/repos/jquery/jquery/branches{/branch}",
        "collaborators_url": "https://api.github.com/repos/jquery/jquery/collaborators{/collaborator}",
        "comments_url": "https://api.github.com/repos/jquery/jquery/comments{/number}",
        "commits_url": "https://api.github.com/repos/jquery/jquery/commits{/sha}",
        "compare_url": "https://api.github.com/repos/jquery/jquery/compare/{base}...{head}",
        "contents_url": "https://api.github.com/repos/jquery/jquery/contents/{+path}",
        "contributors_url": "https://api.github.com/repos/jquery/jquery/contributors",
        "deployments_url": "http://api.github.com/repos/octocat/Hello-World/deployments",
        "description": "jQuery JavaScript Library",
        "downloads_url": "https://api.github.com/repos/jquery/jquery/downloads",
        "events_url": "https://api.github.com/repos/jquery/jquery/events",
        "fork": false,
        "forks_url": "https://api.github.com/repos/jquery/jquery/forks",
        "full_name": "jquery/jquery",
        "git_commits_url": "https://api.github.com/repos/jquery/jquery/git/commits{/sha}",
        "git_refs_url": "https://api.github.com/repos/jquery/jquery/git/refs{/sha}",
        "git_tags_url": "https://api.github.com/repos/jquery/jquery/git/tags{/sha}",
        "hooks_url": "https://api.github.com/repos/jquery/jquery/hooks",
        "html_url": "https://github.com/jquery/jquery",
        "id": 167174,
        "issue_comment_url": "https://api.github.com/repos/jquery/jquery/issues/comments/{number}",
        "issue_events_url": "https://api.github.com/repos/jquery/jquery/issues/events{/number}",
        "issues_url": "https://api.github.com/repos/jquery/jquery/issues{/number}",
        "keys_url": "https://api.github.com/repos/jquery/jquery/keys{/key_id}",
        "labels_url": "https://api.github.com/repos/jquery/jquery/labels{/name}",
        "languages_url": "https://api.github.com/repos/jquery/jquery/languages",
        "merges_url": "https://api.github.com/repos/jquery/jquery/merges",
        "milestones_url": "https://api.github.com/repos/jquery/jquery/milestones{/number}",
        "name": "jquery",
        "node_id": "MDEwOlJlcG9zaXRvcnkxNjcxNzQ=",
        "notifications_url": "https://api.github.com/repos/jquery/jquery/notifications{?since,all,participating}",
        "owner": {
          "avatar_url": "https://0.gravatar.com/avatar/6906f317a4733f4379b06c32229ef02f?d=https%3A%2F%2Fidenticons.github.com%2Ff426f04f2f9813718fb806b30e0093de.png",
          "events_url": "https://api.github.com/users/jquery/events{/privacy}",
          "followers_url": "https://api.github.com/users/jquery/followers",
          "following_url": "https://api.github.com/users/jquery/following{/other_user}",
          "gists_url": "https://api.github.com/users/jquery/gists{/gist_id}",
          "gravatar_id": "",
          "html_url": "https://github.com/jquery",
          "id": 70142,
          "login": "jquery",
          "node_id": "MDQ6VXNlcjcwMTQy",
          "organizations_url": "https://api.github.com/users/jquery/orgs",
          "received_events_url": "https://api.github.com/users/jquery/received_events",
          "repos_url": "https://api.github.com/users/jquery/repos",
          "site_admin": false,
          "starred_url": "https://api.github.com/users/jquery/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/jquery/subscriptions",
          "type": "Organization",
          "url": "https://api.github.com/users/jquery"
        },
        "private": false,
        "pulls_url": "https://api.github.com/repos/jquery/jquery/pulls{/number}",
        "releases_url": "http://api.github.com/repos/octocat/Hello-World/releases{/id}",
        "stargazers_url": "https://api.github.com/repos/jquery/jquery/stargazers",
        "statuses_url": "https://api.github.com/repos/jquery/jquery/statuses/{sha}",
        "subscribers_url": "https://api.github.com/repos/jquery/jquery/subscribers",
        "subscription_url": "https://api.github.com/repos/jquery/jquery/subscription",
        "tags_url": "https://api.github.com/repos/jquery/jquery/tags",
        "teams_url": "https://api.github.com/repos/jquery/jquery/teams",
        "trees_url": "https://api.github.com/repos/jquery/jquery/git/trees{/sha}",
        "url": "https://api.github.com/repos/jquery/jquery"
      },
      "score": 1,
      "sha": "d7212f9dee2dcc18f084d7df8f417b80846ded5a",
      "url": "https://api.github.com/repositories/167174/contents/src/attributes/classes.js?ref=825ac3773694e0cd23ee74895fd5aeb535b27da4"
    }
  ],
  "total_count": 7
}
```

```json
{"properties":{"incomplete_results":{"type":"boolean"},"items":{"items":{"$ref":"#/components/schemas/code-search-result-item"},"type":"array"},"total_count":{"type":"integer"}},"required":["total_count","incomplete_results","items"],"type":"object"}
```

### `304`

Not modified

### `403`

Forbidden

### `application/json`

- Type: `object`

Basic Error

```json
{"description":"Basic Error","properties":{"documentation_url":{"type":"string"},"message":{"type":"string"},"status":{"type":"string"},"url":{"type":"string"}},"title":"Basic Error","type":"object"}
```

### `422`

Validation failed

### `application/json`

- Type: `object`

Validation Error

```json
{"description":"Validation Error","properties":{"documentation_url":{"type":"string"},"errors":{"items":{"properties":{"code":{"type":"string"},"field":{"type":"string"},"index":{"type":"integer"},"message":{"type":"string"},"resource":{"type":"string"},"value":{"oneOf":[{"nullable":true,"type":"string"},{"nullable":true,"type":"integer"},{"items":{"type":"string"},"nullable":true,"type":"array"}]}},"required":["code"],"type":"object"},"type":"array"},"message":{"type":"string"}},"required":["message","documentation_url"],"title":"Validation Error","type":"object"}
```

### `503`

Service unavailable

### `application/json`

- Type: `object`

```json
{"properties":{"code":{"type":"string"},"documentation_url":{"type":"string"},"message":{"type":"string"}},"type":"object"}
```

## Request examples

### cURL

```shell
curl --request GET \
  --url {protocol}://{hostname}/api/v3/search/code
```
