SDKs overview
The Netlify API provides a programmatic interface for managing Netlify platform resources, including sites, deployments, forms, and users. Developers can use the API to automate continuous deployment pipelines, integrate Netlify features into custom applications, or build specialized tools for managing web projects. While Netlify offers an official SDK in Go, the RESTful nature of its API allows for integration using standard HTTP clients in any programming language. The API follows REST architectural principles, emphasizing stateless operations and standard HTTP methods, which simplifies client-side implementation.
Authentication for the Netlify API primarily relies on Personal Access Tokens, which can be generated and managed directly from the Netlify dashboard. For applications requiring user authorization without exposing personal tokens, Netlify also supports OAuth 2.0, enabling third-party applications to request specific permissions from Netlify users. This dual approach accommodates both direct scripting and more complex application integrations. The Netlify API reference provides detailed endpoint documentation, including request and response schemas, and often includes interactive examples to facilitate development.
Official SDKs by language
Netlify maintains an official SDK for Go, which provides a structured and type-safe way to interact with the Netlify API from Go applications. This SDK abstracts away the complexities of HTTP requests, JSON serialization, and error handling, offering a client object with methods corresponding to various API endpoints. The official Go SDK is designed for stability and full feature coverage, aligning with the latest API versions and best practices for Go development.
While the official SDK is currently focused on Go, developers working in other languages can directly consume the REST API using standard HTTP client libraries. For instance, JavaScript developers commonly use fetch or libraries like Axios, while Python developers might use requests to interact with Netlify's endpoints. This flexibility ensures that the Netlify API is accessible across a wide range of development environments, even without language-specific official SDKs.
Official SDK Table
| Language | Package Name | Install Command (Example) | Maturity | Description |
|---|---|---|---|---|
| Go | github.com/netlify/open-api/go/porcelain |
go get github.com/netlify/open-api/go/porcelain |
Stable | Official client for the Netlify API, providing structured access to all endpoints. |
Installation
Installing the official Netlify Go SDK involves using Go's module management system. The process typically requires a working Go development environment. Once installed, the package can be imported into a Go project, and an API client can be initialized with an authentication token.
Go SDK Installation
To install the official Netlify Go SDK, open your terminal or command prompt and execute the following command:
go get github.com/netlify/open-api/go/porcelain
This command downloads the necessary packages and adds them to your Go module dependencies. After installation, you can import the porcelain package into your Go source files. Ensure your go.mod file is correctly updated, or run go mod tidy to clean up dependencies.
Manual API Integration (for other languages)
For languages without an official SDK, integration involves making direct HTTP requests to the Netlify API endpoints. This typically requires an HTTP client library appropriate for the language. For example, in Node.js, you might use node-fetch or Axios. In Python, the requests library is a common choice. The general steps involve:
- Choose an HTTP Client: Select a suitable library for your programming language (e.g., Axios for JavaScript,
requestsfor Python,Guzzlefor PHP). - Construct Request URLs: Refer to the Netlify API documentation for specific endpoint paths.
- Set Headers: Include an
Authorizationheader with your Personal Access Token (e.g.,Authorization: Bearer YOUR_NETLIFY_ACCESS_TOKEN) and aContent-Type: application/jsonheader for POST/PUT requests. - Send Request: Execute the HTTP request (GET, POST, PUT, DELETE) with any required JSON payload.
- Handle Response: Parse the JSON response and handle any API errors.
Quickstart example
This quickstart example demonstrates how to use the official Netlify Go SDK to fetch a list of sites associated with your account. This requires a Netlify Personal Access Token, which should be treated as sensitive information and not hardcoded directly into source files in production environments. Environment variables or secure configuration management are recommended.
Go SDK Quickstart: Listing Sites
First, ensure you have the Go SDK installed as described in the installation section. Then, create a .go file (e.g., main.go) and add the following code:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/netlify/open-api/go/porcelain"
)
func main() {
accessToken := os.Getenv("NETLIFY_ACCESS_TOKEN")
if accessToken == "" {
log.Fatal("NETLIFY_ACCESS_TOKEN environment variable not set")
}
client := porcelain.NewClient(accessToken)
sites, _, err := client.ListSites(context.Background(), nil)
if err != nil {
log.Fatalf("Error listing sites: %v", err)
}
fmt.Println("Your Netlify Sites:")
for _, site := range sites {
fmt.Printf("- %s (ID: %s, URL: %s)\n", site.Name, site.Id, *site.Url)
}
}
Before running this code, set your Netlify Personal Access Token as an environment variable named NETLIFY_ACCESS_TOKEN:
export NETLIFY_ACCESS_TOKEN="YOUR_NETLIFY_PERSONAL_ACCESS_TOKEN"
go run main.go
Replace "YOUR_NETLIFY_PERSONAL_ACCESS_TOKEN" with your actual token. This program will then print the names, IDs, and URLs of all sites accessible with that token.
JavaScript Quickstart (fetch API): Listing Sites
For developers using JavaScript, interaction with the Netlify API typically involves using the browser's native fetch API or a library like Axios. This example demonstrates fetching sites using fetch.
async function listNetlifySites() {
const accessToken = process.env.NETLIFY_ACCESS_TOKEN; // Or retrieve securely
if (!accessToken) {
console.error("NETLIFY_ACCESS_TOKEN environment variable not set");
return;
}
try {
const response = await fetch('https://api.netlify.com/api/v1/sites', {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const sites = await response.json();
console.log('Your Netlify Sites:');
sites.forEach(site => {
console.log(`- ${site.name} (ID: ${site.id}, URL: ${site.url})`);
});
} catch (error) {
console.error('Error listing sites:', error);
}
}
// Call the function
listNetlifySites();
To run this in a Node.js environment, save it as listSites.js and execute it after setting the environment variable:
export NETLIFY_ACCESS_TOKEN="YOUR_NETLIFY_PERSONAL_ACCESS_TOKEN"
node listSites.js
This JavaScript example performs the same operation as the Go example, demonstrating the direct use of the REST API for languages without a dedicated official SDK.
Community libraries
Beyond the official Go SDK, the Netlify community has developed various tools and libraries to simplify interactions with the Netlify API and integrate Netlify features into different development workflows. These community-contributed resources often address specific use cases, provide abstractions for particular frameworks, or offer support for languages where an official SDK is not available.
While Netlify's official documentation primarily focuses on direct API interaction and the Go SDK, platforms like GitHub host numerous community projects. Searching for "Netlify API client" or "Netlify SDK" on GitHub can reveal open-source libraries in languages such as JavaScript, Python, and Ruby. These libraries vary in maturity, feature completeness, and maintenance status. Developers should review the repository's activity, documentation, and issue tracker before adopting a community library for production use.
Examples of community contributions often include:
- JavaScript/Node.js Clients: Libraries that wrap the REST API with promise-based or async/await functions, making it easier to use in Node.js applications or client-side JavaScript. Some might focus on specific API features like forms or deployment management.
- CLI Tools: While Netlify provides an official Netlify CLI, community CLIs might offer specialized functionality or integrations with other tools.
- Framework-Specific Integrations: Libraries designed to integrate Netlify API features directly into popular web frameworks, simplifying tasks like dynamic site generation or serverless function deployment.
- Utility Scripts: Collections of scripts in various languages for common Netlify automation tasks, such as bulk site updates, deployment monitoring, or analytics extraction.
When considering a community library, it is advisable to check its documentation, the activity of its maintainers, and its compatibility with the latest Netlify API versions to ensure reliable operation and ongoing support. The Netlify community forums and official GitHub repositories can also be valuable resources for discovering and discussing these projects.