Skip to main content

Search code

Searches for query terms inside of a file. This method returns up to 100 results [per page](https://docs.github.com/[email protected]/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/[email protected]/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.

GET/search/code
Source
github-v3-rest.json
Revision
f5f067c1e6f6
Active snapshot
85e53e3d0d65

Request

Query Parameters
q string
required

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/[email protected]/rest/reference/search#constructing-a-search-query). See "[Searching code](https://docs.github.com/articles/searching-code/)" for a detailed list of qualifiers.

sort string
optional

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/[email protected]/rest/reference/search#ranking-search-results)

order string
optional

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 integer
optional

Results per page (max 100)

page integer
optional

Page number of the results to fetch.

Responses

application/json object

Response

incomplete_results boolean
required
items array[Code Search Result Item object]
required
items Code Search Result Item object

Code Search Result Item

file_size integer
optional
git_url string<uri>
required
html_url string<uri>
required
language string
optionalnullable
last_modified_at string<date-time>
optional
line_numbers array[string]
optional
Example:
[
  "73..77",
  "77..78"
]
name string
required
path string
required
repository Minimal Repository object
required

Minimal Repository

allow_forking boolean
optional
archive_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}

archived boolean
optional
assignees_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/assignees{/user}

blobs_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}

branches_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/branches{/branch}

clone_url string
optional
code_of_conduct Code Of Conduct object
optional

Code Of Conduct

collaborators_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}

comments_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/comments{/number}

commits_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/commits{/sha}

compare_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}

contents_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/contents/{+path}

contributors_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/contributors

created_at string<date-time>
optionalnullable

Example: 2011-01-26T19:01:12Z

default_branch string
optional
delete_branch_on_merge boolean
optional
deployments_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/deployments

description string
requirednullable

Example: This your first repo!

disabled boolean
optional
downloads_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/downloads

events_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/events

fork boolean
required
forks integer
optional

Example: 0

forks_count integer
optional
forks_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/forks

full_name string
required

Example: octocat/Hello-World

git_commits_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/git/commits{/sha}

git_refs_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/git/refs{/sha}

git_tags_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/git/tags{/sha}

git_url string
optional
has_downloads boolean
optional
has_issues boolean
optional
has_pages boolean
optional
has_projects boolean
optional
has_wiki boolean
optional
homepage string
optionalnullable
hooks_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/hooks

html_url string<uri>
required

Example: https://github.com/octocat/Hello-World

id integer
required

Example: 1296269

is_template boolean
optional
issue_comment_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/issues/comments{/number}

issue_events_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/issues/events{/number}

issues_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/issues{/number}

keys_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/keys{/key_id}

labels_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/labels{/name}

language string
optionalnullable
languages_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/languages

license object
optionalnullable
merges_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/merges

milestones_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/milestones{/number}

mirror_url string
optionalnullable
name string
required

Example: Hello-World

network_count integer
optional
node_id string
required

Example: MDEwOlJlcG9zaXRvcnkxMjk2MjY5

notifications_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}

open_issues integer
optional

Example: 0

open_issues_count integer
optional
owner Simple User object
required

Simple User

permissions object
optional
private boolean
required
pulls_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/pulls{/number}

pushed_at string<date-time>
optionalnullable

Example: 2011-01-26T19:06:43Z

releases_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/releases{/id}

size integer
optional
ssh_url string
optional
stargazers_count integer
optional
stargazers_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/stargazers

statuses_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/statuses/{sha}

subscribers_count integer
optional
subscribers_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/subscribers

subscription_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/subscription

svn_url string
optional
tags_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/tags

teams_url string<uri>
required

Example: http://api.github.com/repos/octocat/Hello-World/teams

temp_clone_token string
optional
template_repository Repository object
optionalnullable

A git repository

topics array
optional
trees_url string
required

Example: http://api.github.com/repos/octocat/Hello-World/git/trees{/sha}

updated_at string<date-time>
optionalnullable

Example: 2011-01-26T19:14:43Z

url string<uri>
required

Example: https://api.github.com/repos/octocat/Hello-World

visibility string
optional
watchers integer
optional

Example: 0

watchers_count integer
optional
score number
required
sha string
required
text_matches Search Result Text Matches array[object]
optional
url string<uri>
required
total_count integer
required

Not modified

application/json Basic Error object

Forbidden

documentation_url string
optional
message string
optional
status string
optional
url string
optional
application/json Validation Error object

Validation failed

documentation_url string
required
errors array[object]
optional
items object
code string
required
field string
optional
index integer
optional
message string
optional
resource string
optional
value
optional
message string
required
application/json object

Service unavailable

code string
optional
documentation_url string
optional
message string
optional