Skip to content

writerai/writer-client-sdk-go

Repository files navigation

Go SDK

AI for everyone.

SDK Installation

go get github.com/writerai/writer-client-sdk-go

Authentication

Writer authenticates your API requests using your account’s API keys. If you do not include your key when making an API request, or use one that is incorrect or outdated, Writer returns an error.

Your API keys are available in the account dashboard. We include randomly generated API keys in our code examples if you are not logged in. Replace these with your own or log in to see code examples populated with your own API keys.

writer-auth

If you cannot see your secret API keys in the Dashboard, this means you do not have access to them. Contact your Writer account owner and ask to be added to their team as a developer.

SDK Example Usage

Example

package main

import (
	"context"
	writerclientsdkgo "github.com/writerai/writer-client-sdk-go"
	"github.com/writerai/writer-client-sdk-go/pkg/models/shared"
	"log"
)

func main() {
	s := writerclientsdkgo.New(
		writerclientsdkgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		writerclientsdkgo.WithOrganizationID(850421),
	)

	ctx := context.Background()
	res, err := s.Billing.GetSubscriptionDetails(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.SubscriptionPublicResponseAPI != nil {
		// handle response
	}
}

Available Resources and Operations

  • Detect - Content detector api
  • Check - Check your content against your preset styleguide.
  • Correct - Apply the style guide suggestions directly to your content.
  • List - List available LLM models
  • Create - Create model customization
  • Delete - Delete Model customization
  • Get - Get model customization
  • List - List model customizations
  • FetchFile - Download your fine-tuned model (available only for Palmyra Base and Palmyra Large)
  • Get - Get document details
  • List - List team documents

Special Types

Global Parameters

A parameter is configured globally. This parameter must be set on the SDK client instance itself during initialization. When configured as an option during SDK initialization, This global value will be used as the default on the operations that use it. When such operations are called, there is a place in each to override the global value, if needed.

For example, you can set organizationId to 99895 at SDK initialization and then you do not have to pass the same value on calls to operations like Detect. But if you want to do so you may, which will locally override the global setting. See the example code below for a demonstration.

Available Globals

The following global parameter is available. The required parameter must be set when you initialize the SDK client.

Name Type Required Description
OrganizationID int64 ✔️ The OrganizationID parameter.

Example

package main

import (
	"context"
	writerclientsdkgo "github.com/writerai/writer-client-sdk-go"
	"github.com/writerai/writer-client-sdk-go/pkg/models/shared"
	"log"
)

func main() {
	s := writerclientsdkgo.New(
		writerclientsdkgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		writerclientsdkgo.WithOrganizationID(496531),
	)

	contentDetectorRequest := shared.ContentDetectorRequest{
		Input: "<value>",
	}

	var organizationID *int64 = writerclientsdkgo.Int64(592237)

	ctx := context.Background()
	res, err := s.AIContentDetector.Detect(ctx, contentDetectorRequest, organizationID)
	if err != nil {
		log.Fatal(err)
	}
	if res.Classes != nil {
		// handle response
	}
}

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both. When specified by the OpenAPI spec document, the SDK will return the appropriate subclass.

Error Object Status Code Content Type
sdkerrors.FailResponse 400,401,403,404,500 application/json
sdkerrors.SDKError 4xx-5xx /

Example

package main

import (
	"context"
	"errors"
	writerclientsdkgo "github.com/writerai/writer-client-sdk-go"
	"github.com/writerai/writer-client-sdk-go/pkg/models/sdkerrors"
	"github.com/writerai/writer-client-sdk-go/pkg/models/shared"
	"log"
)

func main() {
	s := writerclientsdkgo.New(
		writerclientsdkgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		writerclientsdkgo.WithOrganizationID(850421),
	)

	ctx := context.Background()
	res, err := s.Billing.GetSubscriptionDetails(ctx)
	if err != nil {

		var e *sdkerrors.FailResponse
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *sdkerrors.SDKError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Server Selection

Select Server by Index

You can override the default server globally using the WithServerIndex option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Variables
0 https://enterprise-api.writer.com None

Example

package main

import (
	"context"
	writerclientsdkgo "github.com/writerai/writer-client-sdk-go"
	"github.com/writerai/writer-client-sdk-go/pkg/models/shared"
	"log"
)

func main() {
	s := writerclientsdkgo.New(
		writerclientsdkgo.WithServerIndex(0),
		writerclientsdkgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		writerclientsdkgo.WithOrganizationID(850421),
	)

	ctx := context.Background()
	res, err := s.Billing.GetSubscriptionDetails(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.SubscriptionPublicResponseAPI != nil {
		// handle response
	}
}

Override Server URL Per-Client

The default server can also be overridden globally using the WithServerURL option when initializing the SDK client instance. For example:

package main

import (
	"context"
	writerclientsdkgo "github.com/writerai/writer-client-sdk-go"
	"github.com/writerai/writer-client-sdk-go/pkg/models/shared"
	"log"
)

func main() {
	s := writerclientsdkgo.New(
		writerclientsdkgo.WithServerURL("https://enterprise-api.writer.com"),
		writerclientsdkgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		writerclientsdkgo.WithOrganizationID(850421),
	)

	ctx := context.Background()
	res, err := s.Billing.GetSubscriptionDetails(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.SubscriptionPublicResponseAPI != nil {
		// handle response
	}
}

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"
	"github.com/myorg/your-go-sdk"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = sdk.New(sdk.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
APIKey apiKey API key

You can configure it using the WithSecurity option when initializing the SDK client instance. For example:

package main

import (
	"context"
	writerclientsdkgo "github.com/writerai/writer-client-sdk-go"
	"log"
)

func main() {
	s := writerclientsdkgo.New(
		writerclientsdkgo.WithSecurity("<YOUR_API_KEY_HERE>"),
		writerclientsdkgo.WithOrganizationID(850421),
	)

	ctx := context.Background()
	res, err := s.Billing.GetSubscriptionDetails(ctx)
	if err != nil {
		log.Fatal(err)
	}
	if res.SubscriptionPublicResponseAPI != nil {
		// handle response
	}
}

SDK Generated by Speakeasy