Documentation
¶
Overview ¶
Package stackin is the official Go SDK for the stackin fiscal document API — issue, consult, and cancel Brazilian NF-e and NFS-e electronic invoices with a single API key, no certificates or XML handling required.
See https://docs.stackin.io for the REST API reference and https://docs.stackin.io/blog/go-sdk for the Go SDK guide.
Index ¶
- Constants
- Variables
- type APIError
- type Address
- type ConnectionFailedError
- type Direction
- type DocumentType
- type Environment
- type Export
- type FiscalReference
- type HistoryQuery
- type InvalidationRequest
- type Invoice
- func (inv *Invoice) Cancel(accessKey string, documentType DocumentType, reason string, ...) (map[string]any, error)
- func (inv *Invoice) Consult(accessKey string, documentType DocumentType) (map[string]any, error)
- func (inv *Invoice) Correct(accessKey string, documentType DocumentType, correction string) (map[string]any, error)
- func (inv *Invoice) History(query HistoryQuery) (map[string]any, error)
- func (inv *Invoice) Invalidate(req InvalidationRequest) (map[string]any, error)
- func (inv *Invoice) Issue(req IssueRequest) (map[string]any, error)
- func (inv *Invoice) Manifest(accessKey string, manifestation Manifestation, reason string) (map[string]any, error)
- func (inv *Invoice) Pdf(accessKey string, documentType DocumentType) ([]byte, error)
- func (inv *Invoice) Received(limit, offset int) (map[string]any, error)
- func (inv *Invoice) Reissue(invoiceID string, opts ...RequestOption) (map[string]any, error)
- func (inv *Invoice) Submissions(invoiceID string) ([]map[string]any, error)
- type InvoiceError
- type IssueRequest
- type Kind
- type Manifestation
- type Option
- type Purpose
- type RequestOption
- type SearchQuery
- type Taxpayer
Constants ¶
const AbroadState = "EX"
AbroadState is the State of a buyer abroad on an NF-e. Such an address may leave ZipCode and CityCode out and names its country with CountryCode (BACEN code) and CountryName instead.
const DefaultBaseURL = "https://sdk.stackin.io"
Variables ¶
var Kinds = []string{
"cfop",
"ncm",
"cest",
"cst",
"csosn",
"iss_service",
"icms_fuel",
"ibs_cbs_class",
}
Kinds are the classifications with a named accessor on FiscalReference. It is not the whole truth and is not meant to be: ask Kinds() for what the API actually has, because a classification published after this release is reachable through Kind() with no release at all.
Functions ¶
This section is empty.
Types ¶
type Address ¶
type Address struct {
State string `json:"state,omitempty"`
CityCode string `json:"city_code,omitempty"`
Street string `json:"street,omitempty"`
Number string `json:"number,omitempty"`
Neighborhood string `json:"neighborhood,omitempty"`
City string `json:"city,omitempty"`
ZipCode string `json:"zip_code,omitempty"`
CountryCode string `json:"country_code,omitempty"`
CountryName string `json:"country_name,omitempty"`
}
type ConnectionFailedError ¶
type ConnectionFailedError struct {
Message string
}
func (*ConnectionFailedError) Error ¶
func (e *ConnectionFailedError) Error() string
type Direction ¶ added in v0.14.0
type Direction string
Direction is whether an NF-e is outgoing or an incoming one the company issues itself (tpNF). The API defaults to DirectionOut when it is left out.
type DocumentType ¶
type DocumentType string
const ( NFE DocumentType = "nfe" NFSE DocumentType = "nfse" )
type Environment ¶
type Environment string
const ( Local Environment = "local" Test Environment = "test" Production Environment = "production" )
type Export ¶ added in v0.14.0
type Export struct {
ExitState string `json:"exit_state"`
Place string `json:"place"`
DispatchPlace string `json:"dispatch_place,omitempty"`
}
Export is where the goods of an NF-e to a buyer abroad leave Brazil (the exporta group): ExitState is a Brazilian state (UFSaidaPais, never EX), Place the place of shipment or border crossing (xLocExporta) and DispatchPlace the optional customs facility of dispatch (xLocDespacho).
type FiscalReference ¶ added in v0.10.0
type FiscalReference struct {
CFOP *Kind
NCM *Kind
CEST *Kind
CST *Kind
CSOSN *Kind
IssService *Kind
IcmsFuel *Kind
IbsCbsClass *Kind
// contains filtered or unexported fields
}
FiscalReference reads the published classification tables. Nothing here is the company's own data and nothing here is writable.
func NewFiscalReference ¶ added in v0.10.0
func NewFiscalReference(opts ...Option) *FiscalReference
func (*FiscalReference) Kind ¶ added in v0.10.0
func (r *FiscalReference) Kind(name string, country ...string) *Kind
Kind reaches any classification by name, including one with no accessor on this struct.
func (*FiscalReference) Kinds ¶ added in v0.10.0
func (r *FiscalReference) Kinds(country ...string) ([]string, error)
Kinds asks which classifications this country has rows for. The only honest answer to "what else is there" — a hard-coded list goes stale the next time the ETL grows one.
func (*FiscalReference) Search ¶ added in v0.10.0
func (r *FiscalReference) Search(query SearchQuery) (map[string]any, error)
Search spans every classification at once, which no accessor can express. It is the most expensive call the endpoint accepts: the whole country's tables, not one of them.
type HistoryQuery ¶ added in v0.5.0
type HistoryQuery struct {
DocumentType DocumentType
Status string
Limit int
Offset int
SortBy string
OrderBy string
}
HistoryQuery filters the issuance history. Every field is optional; the zero value asks for the newest page with the API's own defaults.
type InvalidationRequest ¶ added in v0.2.0
type Invoice ¶
type Invoice struct {
// contains filtered or unexported fields
}
func NewInvoice ¶
func (*Invoice) Cancel ¶
func (inv *Invoice) Cancel(accessKey string, documentType DocumentType, reason string, opts ...RequestOption) (map[string]any, error)
Cancel voids an authorized document. Pass WithIdempotencyKey to make a retry safe: repeating the same key with the same body replays the first answer instead of cancelling twice. This is the one irreversible operation here, with a legal window and no undo.
func (*Invoice) Correct ¶ added in v0.2.0
func (inv *Invoice) Correct(accessKey string, documentType DocumentType, correction string) (map[string]any, error)
Correct files a correction letter (CC-e). NF-e only: a key that is not an NF-e access key is refused before the call. A well-formed key sent with NFSE, or one for a document the API does not hold, is answered 409 by the API and comes back as an *APIError.
func (*Invoice) History ¶ added in v0.5.0
func (inv *Invoice) History(query HistoryQuery) (map[string]any, error)
History lists what this company issued, newest first.
The counterpart of Received: that one lists what was issued against the company, this one what the company issued.
func (*Invoice) Invalidate ¶ added in v0.2.0
func (inv *Invoice) Invalidate(req InvalidationRequest) (map[string]any, error)
func (*Invoice) Manifest ¶ added in v0.3.1
func (inv *Invoice) Manifest(accessKey string, manifestation Manifestation, reason string) (map[string]any, error)
Manifest files the recipient's formal answer to a received document.
Only OperacaoNaoRealizada takes a reason, and it requires one. Both are fixed rules, checked here rather than spending a round trip to be told.
func (*Invoice) Pdf ¶ added in v0.3.0
func (inv *Invoice) Pdf(accessKey string, documentType DocumentType) ([]byte, error)
Pdf returns the authorizer's own rendering of an authorized document, as raw bytes — the only method that does not return a parsed object. The XML is the legally valid document; this is a convenience, and the authorizer's endpoint for it is unstable, so an APIError with status 502 means the authorizer is unavailable, not that the invoice is wrong. NFS-e only.
func (*Invoice) Received ¶ added in v0.3.1
Received lists documents other companies issued against this one.
Reads what the API already collected; it does not call the authorizer. Collecting runs on a schedule there, because the SEFAZ caps how many times a CNPJ may ask per day.
func (*Invoice) Submissions ¶ added in v0.6.0
Submissions returns every attempt made for one invoice, with what the authorizer answered to each.
Consult gives the status; this gives the reason. It takes the invoiceID, like Reissue and unlike everything else, because a rejected document has no access key to look it up by.
type InvoiceError ¶
type InvoiceError struct {
Message string
}
func (*InvoiceError) Error ¶
func (e *InvoiceError) Error() string
type IssueRequest ¶
type IssueRequest struct {
DocumentType DocumentType
ClientName string
TaxID string
Items []br.Product
RecipientAddress *Address
Series string
Number string
// ExternalReference is the caller's own id for the document, an order
// number say. The API stores it as sent (at most 255 characters) and
// returns it on every read and webhook. Omitted when empty.
ExternalReference string
// IdempotencyKey makes a retry safe: the same key with the same body
// replays the first response instead of issuing a second document.
// Keys live 24 hours. Never generated for you — only the caller knows
// which two requests are meant to be the same one.
IdempotencyKey string
// Discount is a discount on the whole document, in BRL, zero or more.
// The API spreads it over the lines. Omitted when nil.
Discount *float64
// IssuedAt is the emission moment (dhEmi), sent as RFC 3339 in the
// offset of its own Location. NF-e: at most 30 days back and 5 minutes
// ahead; NFS-e: never ahead. The API defaults it to now. Omitted when
// nil.
IssuedAt *time.Time
// Purpose is why the NF-e is issued; Direction whether it is outgoing
// or incoming. NF-e only, omitted when empty.
Purpose Purpose
Direction Direction
// ReferencedKeys are the 44-character access keys of the NF-e, NFC-e
// or CF-e this NF-e refers to: required for PurposeComplementary and,
// but for a few CFOPs, PurposeReturn. NF-e only, omitted when empty.
ReferencedKeys []string
// ForeignID identifies a buyer abroad (idEstrangeiro, 5 to 20
// characters), for an NF-e whose RecipientAddress.State is EX. TaxID
// is then left out. Omitted when empty.
ForeignID string
// Export is where the goods leave Brazil, required for an outgoing
// NF-e to a buyer abroad. Omitted when nil.
Export *Export
}
type Kind ¶ added in v0.10.0
Kind is one classification, bound to a country.
func (*Kind) Get ¶ added in v0.10.0
Get returns one code. A code that does not exist is a 404, which arrives as *APIError — there is no separate not-found type.
func (*Kind) Search ¶ added in v0.10.0
func (k *Kind) Search(query SearchQuery) (map[string]any, error)
Search returns a page of this classification, filtered by the query's term when it has one. An empty term is no term: the route rejects search="" with a 422, and sending it would invent a failure the caller cannot read.
type Manifestation ¶ added in v0.3.1
type Manifestation string
Manifestation is the recipient's answer to a document issued against it.
const ( Confirmacao Manifestation = "210200" Ciencia Manifestation = "210210" Desconhecimento Manifestation = "210220" OperacaoNaoRealizada Manifestation = "210240" )
type Option ¶
type Option func(*transport)
func WithAPIKey ¶
func WithBaseURL ¶
func WithCountry ¶ added in v0.10.0
WithCountry sets the ISO 3166-1 alpha-2 code FiscalReference and Taxpayer look codes up under. Defaults to BR.
func WithEnvironment ¶
func WithEnvironment(env Environment) Option
func WithTimeout ¶
type Purpose ¶ added in v0.14.0
type Purpose string
Purpose is why an NF-e is issued (finNFe). The API defaults to PurposeNormal when it is left out.
type RequestOption ¶ added in v0.2.0
type RequestOption func(*requestConfig)
RequestOption tunes a single call, as opposed to Option, which tunes the client.
func WithIdempotencyKey ¶ added in v0.2.0
func WithIdempotencyKey(key string) RequestOption
WithIdempotencyKey attaches an Idempotency-Key to one call. See IssueRequest.IdempotencyKey for what the API does with it.
type SearchQuery ¶ added in v0.10.0
SearchQuery narrows a classification listing.
There is no SortBy or OrderBy: the route accepts both and discards them, so rows always come back ordered by kind then code. History on Invoice does honour them, which is why the difference is spelled out here rather than left to be discovered.
type Taxpayer ¶ added in v0.10.0
type Taxpayer struct {
// contains filtered or unexported fields
}
Taxpayer reads the taxpayer registry, one tax id at a time.
One method, and it stays one method. The registry names and addresses real people and companies: an exact lookup answers the question an issuer has — who is this CNPJ I am about to invoice — and answers nothing else. A prefix or wildcard search over the same table is a bulk export wearing the costume of a query. Do not add one, not for parity with FiscalReference, not "just by name", not "just within one state".
func NewTaxpayer ¶ added in v0.10.0
func (*Taxpayer) Get ¶ added in v0.10.0
Get returns one taxpayer, by exact tax id.
A 404 arrives as *APIError and does not mean the taxpayer does not exist: the registry reloads monthly, so a company registered in the last few weeks is simply not in it yet. Never build a validation rule on top of it.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
cancel_invoice
command
|
|
|
consult_invoice
command
|
|
|
lookup_fiscal_reference
command
|
|
|
lookup_taxpayer
command
|
|
|
nfe/basic
command
|
|
|
nfe/full
command
|
|
|
nfe/icms_isento
command
|
|
|
nfe/imported
command
|
|
|
nfe/interstate_with_icms_dest
command
|
|
|
nfe/scale_manufactured
command
|
|
|
nfe/taxed_icms
command
|
|
|
nfe/unit_price
command
|
|
|
nfe/used_asset
command
|
|
|
nfe/with_barcode
command
|
|
|
nfe/with_extra_charges
command
|
|
|
nfe/with_purchase_order
command
|
|
|
nfe/with_quantity
command
|
|
|
nfe/with_tax_benefit
command
|
|
|
nfse/basic
command
|
|
|
nfse/full
command
|
|
|
nfse/with_discount
command
|
|
|
nfse/with_observations
command
|
|
|
nfse/with_service_code
command
|
|
|
nfse/with_tax_retained
command
|
|
|
reissue_invoice
command
|