PHP code example of engineapi / sdk
1. Go to this page and download the library: Download engineapi/sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
engineapi / sdk example snippets
use EngineApi\EngineApiClient;
$client = new EngineApiClient([
'base_url' => 'https://api.engineapi.com.br',
'api_key' => 'ek_live_sua_chave_aqui',
]);
// Alternativa: login com email e senha
$client = new EngineApiClient(['base_url' => 'https://api.engineapi.com.br']);
$client->login('[email protected] ', 'sua-senha');
use EngineApi\EngineApiClient;
use EngineApi\EngineApiError;
$client = new EngineApiClient([
'base_url' => 'https://api.engineapi.com.br',
'api_key' => 'ek_test_sua_chave_aqui',
]);
try {
$resposta = $client->nfe->emitir([
'naturezaOperacao' => 'VENDA DE MERCADORIA',
'idDest' => 1,
'indFinal' => 1,
'destinatario' => [
'cnpjCpf' => '99888777000100',
'nome' => 'Cliente Exemplo SA',
'endereco' => [
'logradouro' => 'Av Goias', 'numero' => '500',
'bairro' => 'Centro', 'codigoMunicipio' => '5208707',
'municipio' => 'Goiania', 'uf' => 'GO', 'cep' => '74063010',
],
'indicadorIE' => 9,
],
'items' => [[
'codigo' => 'PROD001',
'descricao' => 'Produto Teste',
'ncm' => '84713012',
'cfop' => '5102',
'unidade' => 'UN',
'quantidade' => 2,
'valorUnitario' => 150.00,
'icms' => ['origem' => 0, 'csosn' => '400'],
]],
'pagamentos' => [['forma' => '01', 'valor' => 300.00]],
]);
$nota = $resposta['data'];
echo "NF-e autorizada: {$nota['accessKey']} ({$nota['status']})\n";
} catch (EngineApiError $e) {
echo "Erro {$e->getStatusCode()}: {$e->getMessage()}\n";
}
$client->nfe->emitir([...]);
$client->nfe->listar(page: 1, limit: 20, status: 'AUTHORIZED');
$client->nfe->consultar('id-da-nota');
$client->nfe->cancelar('chave_de_acesso', 'Erro de digitacao no destinatario');
$client->nfe->cartaCorrecao('chave_de_acesso', 'Correcao do endereco de entrega');
$client->nfe->status();
file_put_contents('danfe.pdf', $client->nfe->downloadPdf('chave_de_acesso'));
file_put_contents('nota.xml', $client->nfe->downloadXml('chave_de_acesso'));
$nfe = $client->nfe->emitir([
// ... demais campos da nota
'items' => [[
'codigo' => 'GLP13',
'descricao' => 'GAS LIQUEFEITO DE PETROLEO BOTIJAO 13KG',
'ncm' => '27111910',
'cfop' => '5656',
'unidade' => 'kg',
'quantidade' => 13,
'valorUnitario' => 8.45,
'icms' => [
'origem' => 0,
'cst' => '61', // cobrado anteriormente (revenda)
'qBCMonoRet' => 13, // quantidade tributada, em kg
'adRemICMSRet' => 1.2196, // R$ por kg
'vICMSMonoRet' => 15.85, // 13 × 1,2196
],
'combustivel' => [
'cProdANP' => '210203001',
'descANP' => 'GLP',
'ufConsumo' => 'GO',
'pGLP' => 60.5,
'pGNn' => 39.5,
'pGNi' => 0,
'vPart' => 4.35,
],
]],
]);
$empresa = $client->companies->criar([
'cnpj' => '99888777000100',
'name' => 'Minha Empresa LTDA',
'crt' => 1, // 1=Simples, 2=Simples com excesso de sublimite, 3=Regime Normal, 4=MEI
'address' => 'Av Goias', 'number' => '500',
'neighborhood' => 'Centro', 'city' => 'Goiania',
'state' => 'GO', 'cep' => '74063010', 'ibgeCode' => '5208707',
]);
// Certificado digital A1 (.pfx), enviado em multipart/form-data
$conteudoPfx = file_get_contents('certificado.pfx');
$client->companies->uploadCertificado($empresa['data']['id'], $conteudoPfx, 'senha-do-certificado');
$client->companies->listar();
$client->companies->buscar('id-da-empresa');
$client->companies->consultarCnpj('99888777000100');
use EngineApi\EngineApiError;
try {
$client->nfe->emitir([...]);
} catch (EngineApiError $e) {
echo "Erro {$e->getStatusCode()}: {$e->getMessage()}\n";
if ($e->isValidationError()) { // 400
print_r($e->getResponse());
} elseif ($e->isUnauthorized()) { // 401
echo "Chave ou token inválido\n";
} elseif ($e->isRateLimited()) { // 429
echo "Limite de requisições atingido\n";
} elseif ($e->isServerError()) { // 5xx
echo "Falha do lado do servidor\n";
}
}