Search code examples
gitapigithubgithub-api

GitHub API: create a repository from template authenticated as installation


I'm attempting to create a repository from a repository template via GitHub's API for an organization. https://api.github.com/repos/{template_org_name}/{template_repo}/generate. I'm trying to do so with a GitHub app authenticated as an installation with administrative permissions on the organization. I'm unable to successfully create a repository and get a Resource not accessible by integration response. I am, however, able to create a repository via this same endpoint using my own GitHub user's personal access token. I'm inclined to think that this endpoint is only available as a user-to-server request, but have not had any luck looking at docs (https://developer.github.com/v3/repos/#create-repository-using-a-repository-template). I understand that it is technically a beta endpoint, so maybe that is my answer.

I checked that I am using the right "Accept" header as well in the request (Accept: application/vnd.github.baptiste-preview+json). Anyone have any luck with this endpoint?


Solution

  • The answers here have become outdated in a good way. At some point between when everyone answered this post to my answer now, GitHub updated the API to allow for this.

    There is an API endpoint that provides this functionality: https://docs.github.com/en/rest/repos/repos#create-a-repository-using-a-template

    Template repos have a /generate endpoint in the API now which allows you send a JSON body with the details of the new repo you want to create.

    curl \
      -X POST \
      -H "Accept: application/vnd.github+json" \ 
      -H "Authorization: Bearer <YOUR-TOKEN>" \
      https://api.github.com/repos/TEMPLATE_OWNER/TEMPLATE_REPO/generate \
      -d '{"owner":"NEW-REPO-OWNER","name":"NEW-REPO-NAME","description":"Repo created from a template","include_all_branches":false,"private":false}'