Search code examples
node.jsnestjs

True layer Api Got Tl-Signature header value is missing error


I am trying to create sandbox payment but not working. I have added header but still got the error. Still I got below error.

Tl-Signature header value is missing from the request

I have attached the code and error response.

Library: https://github.com/TrueLayer/truelayer-signing

docs: https://docs.truelayer.com/reference/create-payment

consolse: https://console.truelayer.com/

import { Injectable } from "@nestjs/common";
const sdk = require('api')('@truelayer/v1.0#7nim1b17lbzalnne');
const sdk1 = require('api')('@truelayer/v1.0#xk1zcylftx71d3');
const tlSigning = require('truelayer-signing');

@Injectable()
export class AppService {

  async getData(){
    sdk.server('https://auth.truelayer-sandbox.com');
    try{
      let data = await sdk.generateAccessToken({
        grant_type: 'client_credentials',
        client_id: 'sandbox-{your id}',
        client_secret: '{your secret}',
        scope: 'payments'
      });
      console.log(data.data)
      const Idempotency = Math.random() * 1000;
      const tlSignature = tlSigning.sign({
        kid: '8ab13f4a-bccc-4c32-b8a8-c1861b377f8e',
        privateKeyPem: ["-----BEGIN EC PRIVATE KEY-----",{your private key},
"-----END EC PRIVATE KEY-----"].join('\n'),
        method: "POST",
        path: "/payments",
        headers: { "Idempotency-Key": Idempotency },
        body: '{"payment_method":{"type":"bank_transfer","provider_selection":{"type":"user_selected","filter":{"release_channel":"general_availability","customer_segments":["retail"]},"scheme_selection":{"type":"instant_only","allow_remitter_fee":false}},"beneficiary":{"type":"merchant_account","verification":{"type":"automated"}}},"amount_in_minor":1,"currency":"GBP"}',
      });
      console.log(tlSignature)
      let res = await sdk1.createPayment('{"payment_method":{"type":"bank_transfer","provider_selection":{"type":"user_selected","filter":{"release_channel":"general_availability","customer_segments":["retail"]},"scheme_selection":{"type":"instant_only","allow_remitter_fee":false}},"beneficiary":{"type":"merchant_account","verification":{"type":"automated"}}},"amount_in_minor":1,"currency":"GBP"}', {
        'Authorization': `Bearer ${data.data.access_token}`,
        'idempotency-key': Idempotency,
        'Tl-Signature': tlSignature,
        'content-type': 'application/json; charset=UTF-8',
      });
      console.log(res)
    }catch(err){
      console.log('error 123')
      console.log(err)
    }
      
    return { message: "Hello API" };
  }
}

Got below Error

Error: Unauthorized
    at new FetchError (/Users/laptop/Documents/Projects/app/node_modules/api/dist/core/errors/fetchError.js:21:28)
    at APICore.<anonymous> (/Users/laptop/Documents/Projects/app/node_modules/api/dist/core/index.js:152:51)
    at step (/Users/laptop/Documents/Projects/app/node_modules/api/dist/core/index.js:44:23)
    at Object.next (/Users/laptop/Documents/Projects/app/node_modules/api/dist/core/index.js:25:53)
    at fulfilled (/Users/laptop/Documents/Projects/app/node_modules/api/dist/core/index.js:16:58)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 401,
  data: {
    type: 'https://docs.truelayer.com/docs/error-types#unauthenticated',
    title: 'Unauthenticated',
    status: 401,
    detail: 'Tl-Signature header value is missing from the request.',
    trace_id: '03bf6cb64bb262a2147fc79c15e3a3f6'
  },
  headers: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(13) {
      'date' => [Object],
      'content-type' => [Object],
      'transfer-encoding' => [Object],
      'connection' => [Object],
      'tl-trace-id' => [Object],
      'referrer-policy' => [Object],
      'x-frame-options' => [Object],
      'x-content-type-options' => [Object],
      'strict-transport-security' => [Object],
      'content-security-policy' => [Object],
      'cf-cache-status' => [Object],
      'server' => [Object],
      'cf-ray' => [Object]
    },
    [Symbol(headers map sorted)]: null
  },
  res: Response {
    [Symbol(realm)]: null,
    [Symbol(state)]: {
      aborted: false,
      rangeRequested: false,
      timingAllowPassed: true,
      requestIncludesCredentials: true,
      type: 'default',
      status: 401,
      timingInfo: [Object],
      cacheState: '',
      statusText: 'Unauthorized',
      headersList: [HeadersList],
      urlList: [Array],
      body: [Object]
    },
    [Symbol(headers)]: HeadersList {
      cookies: null,
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    }
  }
}

Solution

  • Use axios for sending create payment request, instead of library