I have used the Get Repo API (https://api.github.com/repos/myId/myRepoName)to get the repository details. Now I want to create a new repository with the content and files in my "myRepoName".How can I achieve that. Create API(Post)(https://api.github.com/user/repos)
2 Answers
If your source repository is a template repository (https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-template-repository), you can use the following API:
Note: Creating and using repository templates is currently available for developers to preview. To access this new endpoint during the preview period, you must provide a custom media type in the Accept header:
application/vnd.github.baptiste-preview+json
Creates a new repository using a repository template. Use the template_owner and template_repo route parameters to specify the repository to use as the template. The authenticated user must own or be a member of an organization that owns the repository. To check if a repository is available to use as a template, get the repository's information using the Get a repository endpoint and check that the is_template key is true.
POST /repos/:template_owner/:template_repo/generate
Parameters
Name Type Description owner string The organization or person who will own the new repository. To > >create a new repository in an organization, the authenticated user must be a member >of the specified organization. name string Required. The name of the new repository. description string A short description of the new repository. private boolean Either true to create a new private repository or false to >create a new public one. Default: false
https://developer.github.com/v3/repos/#create-a-repository-using-a-template

- 9,878
- 7
- 49
- 66
-
I can only able to create a template api within my github account. what if the template belongs to other account and I want to create a repo template in my github account. – Ashwin Kumar Jun 30 '20 at 10:11
-
It doesn't seem to be possible as per `Use the template_owner and template_repo route parameters to specify the repository to use as the template. The authenticated user must own or be a member of an organization that owns the repository` – riQQ Jun 30 '20 at 13:32
-
Is there is any other way we can use the repo template to create a repo with same content in our git account. – Ashwin Kumar Jun 30 '20 at 14:20
-
As far as I know it's only possible using the GitHub UI – riQQ Jun 30 '20 at 15:56
Using node js v12.18.1 or later and octokit/rest api v17: Just generate a personal access token from your github profile settings and use the code below:
require('dotenv').config();
const { Octokit } = require('@octokit/rest');
const clientWithAuth = new Octokit({
auth: process.env.TOKEN, //create github personal token
});
const main = async () => {
const owner = process.env.OWNER;
const username = process.env.USERNAME;
const response = await clientWithAuth.repos.createUsingTemplate({
template_owner: owner,
template_repo: 'template-testing',
name: `template-${username}`,
});
console.log('repository successfully create ');
};
main();