Link Github Repo & Deploy using Netlify API

Hello Colleagues!
I’m trying to do the following things using Netlify Rest API:

  1. Link my existing Githb Repo that has a Gatsby Starter to Netlify
  2. Deploy that Repo on Netlify

Here is what I do:

  1. Authorize and receive the required auth “Code” from netlify
  2. Fire a POST request to get my deploy_key ID:


  1. I make another POST request to link my GitHub repo with Netlify:${code}`, payload

Here is the “payload” that I pass:
“repo”: {
“provider”: “github”,
“id”: “201622405”,
“repo”: “VasilyMur/defeated-sparkling-mice”,
“repo_url”: ''url",
“private”: false,
“branch”: “master”,
“cmd”: “gatsby build”,
“dir”: “public/”,
“deploy_key_id”: “1111111111111111” <<< id that I get from the previous call

What happens next?

Well, I get a response where I receive confirmation that the Site has been created:
{ id: ‘1111111-1111-1111-1111-11111111111’,
site_id: ‘1111111-1111-1111-1111-11111111111’,
build_id: ‘111111111111111111111111’,
state: ‘current’,
name: ‘distracted-torvalds-e27774’,
url: ‘url’,


  1. Then I finally make a GET request using my Netlify Auth Code (passing the SITE_ID from the previous step):{site_id}/deploys?access_token={code}`

Here I receive status: 200 as well a bunch of other data like: site_id, url, branch and some other stuff.

The I visit my Netlify Admin and see “Deploy failed”.

PM: git ref refs/heads/master does not exist or you do not have permission
So… does it mean I need to get AUTH token from Github and Pass it?
Where should I pass it? Should I add it as a Payload in my “deploys” call somehow??

Please advise me on the right steps I should take in order to finally deploy my site from Github Repo using Netlify’s Rest API.

Thanks in advance!

You shouldn’t need any other auth at that point; the Deploy Key should be all you needed, as long as you actually put the public key from the deploy key in place at GitHub? That is a required step :wink:

If you do think you’re putting the key in place successfully on the repo at GitHub (read only permission to clone code), could you please send me your site ID for one that you’ve configured this way so we can take a look in the database at what you think is a correct linkage to see if we see any problems with the setup you’ve created?

Same to me. I get the deploy_key, and do a post to sites and receave created status. But it happens too git ref refs/heads/master does not exist or you do not have permission.

Any solution for this?

@fagianijunior can you link to one of your failing deploys in the Netlify UI so we can take a look? Thanks

It is my mistake, I need to add the SSH key to github and use the ID to do a deploy. It is OK now. Sorry

@fagianijunior, thank you ( + :100: ) for the follow-up to share the solution which worked for you. If someone else has the same question, your answer above will hopefully with provide them with the information they are seeking.