Support Forums

How to connect to NetlifyAPI using go lib

I’m trying to integrate Netlify API on my go REST API.
I am reading the doc here

I’m fearly new to Go, that’s maybe why I’m having a hard time. But I don’t understand how to create a new client

import netlify "github.com/netlify/open-api/go/porcelain"
    client := netlify.New(transport, strfmt.Default)

As stated in the doc :
func New(transport runtime.ClientTransport, formats strfmt.Registry) *Netlify {

I can’t find any example of connection, I understand that this is from open-api (swagger). But what do I need to send to create this ?
What is the difference with NewHTTPClient ?
and how I use my Netlify API id and secret here ?

I’m sorry for all the questions, but I can’t find any answer online.

Just to add here, since I can’t use the lib I’m trying to use the http library from go. I have read on your documentations that I can create a PAT and use it. But this is not working either, what I’m doing wrong here ?

url := "https://api.netlify.com/api/v1/[company]/sites"
req, err := http.Get(url)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization","bearer [token]")

defer req.Body.Close()

body, err := ioutil.ReadAll(req.Body)
if err != nil {

I also tried this url : https://api.netlify.com/api/v1/sites

I’m getting 401, So I guess something is wrong with my token, but can’t figure out what.

Hey @Frenx,

Unfortunately won’t be able to help too much with the specifics of what you’re trying to achieve, however, our open-API docs in full can be found here and an API demo can be found here. I hope this helps to get you started!

Thank you for the answer @Scott
I managed to get it to work using a PAT and simple API calls with a http client, since we only need to deploy sites with our accounts. We don’t need to grant access to clients.
So far this seems to work fine for us !

1 Like