Search notes:

github Web API: v4 / GraphQL

create repository

The following PowerShell function tries to demonstrate how a repository can be created on GitHub with github's GraphQL interface.
function create-repository($name, $visibility, $token) {
 #
 # Prepare query string with new lines:
 #
   $query_nl = '
      mutation {{
         createRepository (
            input: {{
               name      : \"{0}\",
               visibility:   {1}
            }}
         )
         {{
             clientMutationId
         }}
    }}' -f $name, $visibility

    write-verbose "query with newlines:`n$($query_nl)"

 #
 # Apparently, graphql and/or github does not allow new lines in the query string.
 # So, replace them
 #
   $query = $query_nl.replace("`n", '').replace("`r", '')

   $body = '{{ "query": "{0}" }}' -f $query

   $response = invoke-webrequest `
       https://api.github.com/graphql                          `
      -method         POST                                     `
      -contentType    application/json                         `
      -headers @{Authorization = "Bearer $token"}              `
      -body           $body

 #
 # Note: response might be '200 OK' even if repository was not created.
 #
   write-host "$($response.StatusCode) $($response.StatusDescription)"
}
Github repository github-web-API, path: /v4-GraphQL/create-repository.ps1
After installing the function, it can be executed like so:
$token = '…'
PS:\> create-repository github-web-API PUBLIC $token

See also

Web APIs

Index