# Deploy the github repo using api

**URL:** <https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269>\
**Category:** Support\
**Tags:** deployment, git, netlify-api\
**Created:** [October 12, 2020, 2:38pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269 "2020-10-12T14:38:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [October 12, 2020, 2:38pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/1 "2020-10-12T14:38:17Z")

</div>

**Hi, all.**  
**Hope you are going well.**

**I am new to netlify api. I have referred to netlify api docs. I want to deploy my own github repo using api. There are no info about it.**

**Can anyone guide me to do that.**

**Hoping with a positive response.**

> **Regards, shiva.**

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [October 17, 2020, 8:30pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/2 "2020-10-17T20:30:58Z")

</div>

Hey @shiva,  
Are you interested in deploying a site to Netlify via the API or specifically integrating with Github via the API?

Here’s a blog post about the first:

> **[How to Deploy a Basic Site Using Postman and the Netlify API](https://www.netlify.com/blog/2020/09/24/how-to-deploy-a-simple-site-using-postman-and-the-netlify-api/)**
>
> Learn about deploying sites with the Netlify API and Postman. Get the building blocks you need to make use of our API endpoints and learn about building more complex deploy pipelines.

And here’s a support guide about the second:

> [@\[Support Guide\] Linking a repository via API](https://answers.netlify.com/t/support-guide-linking-a-repository-via-api/121):
>
> Last reviewed by Netlify Support Staff: August 2023 It is easiest to link a repository in Netlify’s UI; doing it via API is challenging and should be a rare use case. Nonetheless, it is possible to create a Netlify site linked to a repository at any of our supported git providers programatically. Specifically, it is possible to use Netlify’s REST API for these situations. You’ll probably be interested in the general topics around “how do I use Netlify’s REST API?” (see [this support guide](https://answers.netlify.com/t/support-guide-understanding-and-using-netlifys-api/160), as w…

Let us know if that helps or if you have other questions!

---

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [October 19, 2020, 9:50am UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/3 "2020-10-19T09:50:00Z")

</div>

Hey, @jen. Thankyou.

It worked fine.

The support guide is worked for me. I have deployed my site which is in GitHub using API.

But I have to do same thing with private GitHub repo. Is there any way to do that.

Hoping with a positive response.

Regards, shiva.

---

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [October 21, 2020, 3:44pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/4 "2020-10-21T15:44:56Z")

</div>

@jen,

Can you please say this

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [October 24, 2020, 4:14pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/5 "2020-10-24T16:14:14Z")

</div>

Hey @shiva,  
Sorry for the delay here! We host many sites that are deployed from private repositories, so linking one should absolutely be possible. Are you running into any errors? Any details about what you’ve tried so far would be super helpful.

---

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [October 25, 2020, 6:19am UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/6 "2020-10-25T06:19:19Z")

</div>

@jen,  
Thanks for the reply.

I used the following code to deploy my site

```auto
    var settings = {
        "url": "https://api.netlify.com/api/v1/sites",
        "method": "POST",
        "timeout": 0,
        "headers": {
          "Authorization": "Bearer "+token,
          "Content-Type": "application/json"
        },
        "data": JSON.stringify({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":false,"branch":"main","deploy_key_id":public_key}}),
      };
      console.log({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":true,"branch":"main","deploy_key_id":public_key}});
      
      $.ajax(settings).done(function (response) {

        console.log(response);

      }).fail(function(e){

          console.log(e);

      });

```

In the above code **sitename** , **token** and **public\_key** are the variables

When I request, I got success response as below

```auto
{"id":"788c169b-799c-4f60-83db-0675bf9d0a7b","site_id":"788c169b-799c-4f60-83db-0675bf9d0a7b","plan":"nf_team_dev","ssl_plan":null,"premium":false,"claimed":true,"name":"dddddddd","custom_domain":null,"domain_aliases":[],"password":null,"notification_email":null,"url":"http://dddddddd.netlify.app","admin_url":"https://app.netlify.com/sites/dddddddd","deploy_id":"","build_id":"5f95155f9026889226bdd1c2","deploy_url":"http://5f9515609026889226bdd1c8--dddddddd.netlify.app","state":"current","screenshot_url":null,"created_at":"2020-10-25T06:04:15.654Z","updated_at":"2020-10-25T06:04:15.713Z","user_id":"5f088353e996d5023cffc741","error_message":null,"ssl":false,"ssl_url":"https://dddddddd.netlify.app","force_ssl":null,"ssl_status":null,"max_domain_aliases":100,"build_settings":{"cmd":null,"dir":null,"env":{},"created_at":"2020-10-25T06:04:15.688Z","updated_at":"2020-10-25T06:04:15.688Z","private_logs":null,"allowed_branches":["main"],"functions_dir":null,"installation_id":null,"skip_prs":null,"untrusted_flow":"review","base_rel_dir":true,"stop_builds":false,"public_repo":true,"provider":"github","repo_type":"git","repo_url":"https://github.com/Shiva-sandupatla/testrepo","repo_branch":"main","repo_path":"Shiva-sandupatla/testrepo","base":null,"deploy_key_id":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDPWw2IqbvzVH2c+J5pY5xIaVZz2SrCtIbfwRSfNu87YIivGtsabN4SRjkyDC4mias8Uc7IIj0c7jDiXX0cAzrILbQQNPPoQeaSb+gOZkYkeeM7dD4UlAPQepT6LBMWZSbQS3+3v+TcyWu501rh4Gn/xoyE5QVE8tQRtHWitfdOw5woMuPO49ZIBlIsociSXvEoGn25W53Jc47nG+TcNoDYbnrsOFDNbJWAoixhd3YeS1N3jiZxU1z9imqf/JZ9RuzWNy91KcT2s6Eg4BjD4umzK1L3liISXdHXUKBzWkurL9+CQTH6lI46F29wbolsInPBL4HeMF8bDxOFh6rjo7KlAYERI0JMkISUm9XByn0t3QGy32BsM6erW8gYYQPlsJnZCjFSz8N5UFJpWXshEErcLKwkC/DC5hEGir2mROXHn8lhowpiOSpYeZtKx4LRdTbp5CUZgD+hIWxnyXyviQKalpXS8z3x2P1wsw9sj/0HTTgMM1scvcZd/nl4HdvYm6FT5ct1pi9vqht1HGaTu3s4QXm+DEyLKg/oIpO6JLbQ5L3mM8q2GD9xyHGdseGcu54/ddtl/O+Hh1Rmz4Uu+ZHRM9K+O1wTuNKXYDyMiI2yLUv9RAV0ZUvhYWKO1j8IYePlyHJPDniZe6gUXfisO2xV+oVPA1yuzEy4K6rvo0Q+fQ=="},"processing_settings":{"css":{"bundle":true,"minify":true},"js":{"bundle":true,"minify":true},"images":{"optimize":true},"html":{"pretty_urls":true},"skip":true,"ignore_html_forms":false},"prerender":null,"prerender_headers":null,"deploy_hook":"https://api.netlify.com/hooks/github","published_deploy":null,"managed_dns":true,"jwt_secret":null,"jwt_roles_path":"app_metadata.authorization.roles","account_slug":"shiva-team","account_name":"Shiva","account_type":"Starter","capabilities":{"title":"Netlify Team Free","asset_acceleration":true,"form_processing":true,"cdn_propagation":"partial","build_node_pool":"buildbot-external-ssd","domain_aliases":true,"secure_site":false,"prerendering":true,"proxying":true,"ssl":"custom","rate_cents":0,"yearly_rate_cents":0,"ipv6_domain":"cdn.makerloop.com","branch_deploy":true,"managed_dns":true,"geo_ip":true,"split_testing":true,"id":"nf_team_dev","cdn_tier":"reg"},"dns_zone_id":null,"identity_instance_id":null,"use_functions":null,"use_edge_handlers":null,"parent_user_id":null,"automatic_tls_provisioning":null,"disabled":null,"lifecycle_state":"active","id_domain":"788c169b-799c-4f60-83db-0675bf9d0a7b.netlify.app","use_lm":null,"build_image":"xenial","automatic_tls_provisioning_expired":false,"analytics_instance_id":null,"functions_region":null,"functions_config":{"site_created_at":"2020-10-25T06:04:15.654Z"},"plugins":[],"default_domain":"dddddddd.netlify.app"}

```

But when i open my dashboard in the website it says deployment failed.

 ![Screenshot (264)](https://global.discourse-cdn.com/netlify/original/3X/a/4/a42647b14d983011b2687caee384a949a83c6aa4.png)

Here is the deploy log:

```auto
11:45:07 AM: Build ready to start
11:45:08 AM: build-image version: b0258b965567defc4a2d7e2f2dec2e00c8f73ad6
11:45:08 AM: build-image tag: v3.4.1
11:45:08 AM: buildbot version: 0a44c930054be748eff0c581d346282fece89cc0
11:45:08 AM: Fetching cached dependencies
11:45:08 AM: Failed to fetch cache, continuing with build
11:45:08 AM: Starting to prepare the repo for build
11:45:09 AM: User git error while checking for ref refs/heads/main
11:45:09 AM: Failing build: Failed to prepare repo
11:45:09 AM: Failed during stage 'preparing repo': error checking for ref: fatal: could not read Username for 'https://github.com': No such device or address: exit status 128
11:45:09 AM: Finished processing build request in 677.152865ms

```

Hoping with a positive response.

Regards, shiva.

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [October 27, 2020, 9:23pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/9 "2020-10-27T21:23:13Z")

</div>

Hey @shiva,  
It looks like you deleted this site since writing in? Please let us know if you have a different site you’d like us to take a look at!

---

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [October 30, 2020, 7:07am UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/10 "2020-10-30T07:07:39Z")

</div>

Hey @jen,

Sorry for that.

Here is the code and response of the website have a look at it.

**code:**

```auto
    var settings = {
        "url": "https://api.netlify.com/api/v1/sites",
        "method": "POST",
        "timeout": 0,
        "headers": {
          "Authorization": "Bearer "+token,
          "Content-Type": "application/json"
        },
        "data": JSON.stringify({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":false,"branch":"main","deploy_key_id":public_key}}),
      };
      console.log({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":true,"branch":"main","deploy_key_id":public_key}});
      
      $.ajax(settings).done(function (response) {

        console.log(response);

      }).fail(function(e){

          console.log(e);

      });

```

**Response:**

```auto
{"id":"2497a7c8-7b21-4c28-b876-a90008c3c172","site_id":"2497a7c8-7b21-4c28-b876-a90008c3c172","plan":"nf_team_dev","ssl_plan":null,"premium":false,"claimed":true,"name":"test-deploy-by-shiva","custom_domain":null,"domain_aliases":[],"password":null,"notification_email":null,"url":"http://test-deploy-by-shiva.netlify.app","admin_url":"https://app.netlify.com/sites/test-deploy-by-shiva","deploy_id":"","build_id":"5f9bbaa8a32410b4bdb40851","deploy_url":"http://5f9bbaa8a32410b4bdb40857--test-deploy-by-shiva.netlify.app","state":"current","screenshot_url":null,"created_at":"2020-10-30T07:03:04.041Z","updated_at":"2020-10-30T07:03:04.085Z","user_id":"5f088353e996d5023cffc741","error_message":null,"ssl":false,"ssl_url":"https://test-deploy-by-shiva.netlify.app","force_ssl":null,"ssl_status":null,"max_domain_aliases":100,"build_settings":{"cmd":null,"dir":null,"env":{},"created_at":"2020-10-30T07:03:04.067Z","updated_at":"2020-10-30T07:03:04.067Z","private_logs":null,"allowed_branches":["main"],"functions_dir":null,"installation_id":null,"skip_prs":null,"untrusted_flow":"review","base_rel_dir":true,"stop_builds":false,"public_repo":true,"provider":"github","repo_type":"git","repo_url":"https://github.com/Shiva-sandupatla/testrepo","repo_branch":"main","repo_path":"Shiva-sandupatla/testrepo","base":null,"deploy_key_id":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGPqkoSwl3Ao+0PQSy5mToe5ZB97EAOnI9PgEo0P+kTNQvpsFQVzQMtsAJkHQqFR+4QR4Pxyw0MnEiKAiHzlnMpmMyRV9q8A/7HTlBj5SjZZRu/wPmyc0uXSlDbUOYvqXvqztckTZJV9sFjXsUIOFiiovEYRUB5fbeZZjHxeGREGqPjjXYSUmCzMBuBbb7PtZsMrbM1yiMy8Vm8DwHQmfOlETnFXIR8dhUPaSrucIsU+xXN7dW6MpQr+4DSAvTE5xSazWNlLIDUDrXvMJjRtPDW7eOBJ08lG9qKyKmIXeAlTWdUKTKn7K3Hi3ywHHi6b8ZF7dTOnE5gxrCpn9sWhleu5QJif0dypKHyCilak9njIjGiq/X6l0ZelnPI591xfPsTxdn0ytky9Ig676SANZvUayfpL7vlGv04VhgBcpo3zWTCBJ1ide4xCetzjD2pRjugtfxoNnh5g5BsauPeimx3Kkfh9WFsvQBbMcBLf7RYoIwaQRMxegDAPj+KFdnMUMflhAgOvrHe32A6x05dyhxHJ9gW5qJzI/1e5hXbT0l2A96JN6k7OtUVp/ei4UyskJRwgT10gKR+WfgEW2Vt2fGh0s4rgCnxDrYx3X8CFqCR5qj/wMMFKps7KSN2WcZrUJ2zGDTMRfWv8j+IDWtXshR2qvre4WYeCWMUM+EWNp9kw=="},"processing_settings":{"css":{"bundle":true,"minify":true},"js":{"bundle":true,"minify":true},"images":{"optimize":true},"html":{"pretty_urls":true},"skip":true,"ignore_html_forms":false},"prerender":null,"prerender_headers":null,"deploy_hook":"https://api.netlify.com/hooks/github","published_deploy":null,"managed_dns":true,"jwt_secret":null,"jwt_roles_path":"app_metadata.authorization.roles","account_slug":"shiva-team","account_name":"Shiva","account_type":"Starter","capabilities":{"title":"Netlify Team Free","asset_acceleration":true,"form_processing":true,"cdn_propagation":"partial","build_node_pool":"buildbot-external-ssd","domain_aliases":true,"secure_site":false,"prerendering":true,"proxying":true,"ssl":"custom","rate_cents":0,"yearly_rate_cents":0,"ipv6_domain":"cdn.makerloop.com","branch_deploy":true,"managed_dns":true,"geo_ip":true,"split_testing":true,"id":"nf_team_dev","cdn_tier":"reg"},"dns_zone_id":null,"identity_instance_id":null,"use_functions":null,"use_edge_handlers":null,"parent_user_id":null,"automatic_tls_provisioning":null,"disabled":null,"lifecycle_state":"active","id_domain":"2497a7c8-7b21-4c28-b876-a90008c3c172.netlify.app","use_lm":null,"build_image":"xenial","automatic_tls_provisioning_expired":false,"analytics_instance_id":null,"functions_region":null,"functions_config":{"site_created_at":"2020-10-30T07:03:04.041Z"},"plugins":[],"default_domain":"test-deploy-by-shiva.netlify.app"}

```

Please have a look at it.

Regards, shiva.

---

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [October 31, 2020, 12:40pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/11 "2020-10-31T12:40:19Z")

</div>

@jen, please have a look at it.

---

<div class="post-metadata">

**Author:** ![jamesalester](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jamesalester/32/17916_2.png) [@jamesalester](https://answers.netlify.com/u/jamesalester)\
**Post date:** [November 1, 2020, 5:10pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/12 "2020-11-01T17:10:26Z")

</div>

Hi @shiva. I may be wrong but I think you’re supposed to use the deploy key id for the `deploy_key_id` argument not the public key which starts with `ssh-rsa`. If you look at the support thread the id they use there is much closer to the `deploy_key_id`.

Having said that, I found this thread because I have a similar issue using the deploy key id:

```
4:39:29 PM: Failed during stage 'preparing repo': error checking for ref: Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
```

---

<div class="post-metadata">

**Author:** ![jamesalester](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jamesalester/32/17916_2.png) [@jamesalester](https://answers.netlify.com/u/jamesalester)\
**Post date:** [November 1, 2020, 5:30pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/13 "2020-11-01T17:30:42Z")

</div>

For anyone with the same issue I had, the GitHub account I was linked to had a restriction on the repositories that the [Netlify GitHub app](https://github.com/settings/installations/) could access. I changed this to all repositories and it worked:

 ![image](https://global.discourse-cdn.com/netlify/original/3X/2/c/2cf68a9e02b1745ec2f7102448fb2235df2aa392.png)

EDIT: I thought this had fixed it as it worked after I changed it but it seems it’s not working for me again and is coming up with the above error again.

EDIT2: Turns out you have to create a deploy key on GitHub using the `public_key` that gets returned with the `deploy_key_id`. You can see how to do this in the [GitHub API docs here](https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#create-a-deploy-key).

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [November 6, 2020, 10:30pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/14 "2020-11-06T22:30:31Z")

</div>

Hey @shiva,  
@jamesalester is absolutely correct, that you will want to use the deploy key **id** in your call to Netlify instead of the public key. There are instructions for getting both values in the support guide I linked above. Please let us know if that doesn’t work for you.

---

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [November 9, 2020, 2:37pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/15 "2020-11-09T14:37:45Z")

</div>

Hey @jen, @jamesalester

I have tried it but I got the logs as below:

```auto
7:40:55 PM: Build ready to start
7:40:57 PM: build-image version: 253f72909528f25a82c4640ca58f2e974d52abd9
7:40:57 PM: build-image tag: v3.4.4
7:40:57 PM: buildbot version: d7fbddb77714d524c4b4de0312067b122124afee
7:40:57 PM: Fetching cached dependencies
7:40:57 PM: Failed to fetch cache, continuing with build
7:40:57 PM: Starting to prepare the repo for build
7:40:57 PM: User git error while checking for ref refs/heads/main
7:40:57 PM: Failing build: Failed to prepare repo
7:40:57 PM: Failed during stage 'preparing repo': error checking for ref: fatal: could not read Username for 'https://github.com': No such device or address: exit status 128
7:40:57 PM: Finished processing build request in 723.608083ms

```

If you don’t mind can you please provide me a detail procedure on deploying my own private repo.

Hoping with a positive response.

Regards, shiva

---

<div class="post-metadata">

**Author:** ![shiva](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shiva/32/16338_2.png) [@shiva](https://answers.netlify.com/u/shiva)\
**Post date:** [November 13, 2020, 4:33pm UTC](https://answers.netlify.com/t/deploy-the-github-repo-using-api/24269/16 "2020-11-13T16:33:53Z")

</div>

Thanks alot @jen & @jamesalester,

I have been trying **deploy\_key\_id** by without putting it in the github deploy keys using api.

In the documentation the procedure is not clear.

In future if anyone want the fully detailed procedure of deploying private repo using netlify api head to [this](https://github.com/Shiva-sandupatla/Netlify-deployment-of-github-repo)

once again i thank @jen & @jamesalester
