code
Creates, updates, deletes, gets or lists a code resource.
Overview
| Name | code |
| Type | Resource |
| Id | github.search.code |
Fields
The following fields are returned by SELECT queries:
- code
Response
| Name | Datatype | Description |
|---|---|---|
incomplete_results | boolean | |
items | array | |
total_count | integer |
Methods
The following methods are available for this resource:
| Name | Accessible by | Required Params | Optional Params | Description |
|---|---|---|---|---|
code | select | q | sort, order, per_page, page | Searches for query terms inside of a file. This method returns up to 100 results per page. 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.For example, if you want to find the definition of the addClass function inside jQuery repository, your query would look something like this:q=addClass+in:file+language:js+repo:jquery/jqueryThis 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 is not valid, while amazing<br />language:go is.> [!NOTE] > repository.description, repository.owner.type, and repository.owner.node_id are closing down on this endpoint and will return null in a future API version. Use the Get a repository endpoint (GET /repos/{owner}/{repo}) to retrieve full repository metadata.This endpoint requires you to authenticate and limits you to 10 requests per minute. |
Parameters
Parameters can be passed in the WHERE clause of a query. Check the Methods section to see which parameters are required or optional for each operation.
| Name | Datatype | Description |
|---|---|---|
q | 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 the web interface for GitHub. To learn more about the format of the query, see Constructing a search query. See "Searching code" for a detailed list of qualifiers. |
order | string | This field is closing down. 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. |
page | integer | The page number of the results to fetch. For more information, see "Using pagination in the REST API." |
per_page | integer | The number of results per page (max 100). For more information, see "Using pagination in the REST API." |
sort | string | This field is closing down. Sorts the results of your query. Can only be indexed, which indicates how recently a file has been indexed by the GitHub search infrastructure. Default: best match |
SELECT examples
- code
Searches for query terms inside of a file. This method returns up to 100 results per page.
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.
For example, if you want to find the definition of the addClass function inside 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 is not valid, while amazing<br />language:go is.
> [!NOTE]
> repository.description, repository.owner.type, and repository.owner.node_id are closing down on this endpoint and will return null in a future API version. Use the Get a repository endpoint (GET /repos/{owner}/{repo}) to retrieve full repository metadata.
This endpoint requires you to authenticate and limits you to 10 requests per minute.
SELECT
incomplete_results,
items,
total_count
FROM github.search.code
WHERE q = '{{ q }}' -- required
AND sort = '{{ sort }}'
AND order = '{{ order }}'
AND per_page = '{{ per_page }}'
AND page = '{{ page }}'
;