@openagenda/sdk-js

OpenAgenda SDK for JavaScript in the browser and Node.js.

Usage no npm install needed!

<script type="module">
  import openagendaSdkJs from 'https://cdn.skypack.dev/@openagenda/sdk-js';
</script>

README

OpenAgenda SDK for JavaScript in the browser and Node.js.

Summary

Installation

yarn add @openagenda/sdk-js

or

npm i @openagenda/sdk-js

Configuration

In the following examples we use async / await, you can use the promises if your environment does not allow it.

Before any operation you have to connect, the token is refreshed automatically as soon as it is necessary.

const OaSdk = require('@openagenda/sdk-js');

const oa = new OaSdk( {
  publicKey: 'YOUR-PUBLIC-KEY',
  secretKey: 'YOUR-PRIVATE-KEY'
} );
await oa.connect();

The public key is used for get method.
The private key is used for all create, update, and delete methods.

API

For more information about data formats please refer to the API documentation on our help center.

Events

get

const eventUid = 87654321;

const event = await oa.events.get( eventUid );

create

const agendaUid = 12345678;

const { success, event } = await oa.events.create( agendaUid, {
  slug: 'a-title',
  title: {
    fr: 'Un titre',
    en: 'A title'
  },
  description: {
    fr: 'La description de votre événement',
    en: 'The description of your event'
  },
  locationUid: 78372099,
  timings: [ {
    begin: moment(),
    end: moment().add( 1, 'hour' )
  }, {
    begin: moment().add( 1, 'day' ),
    end: moment().add( 1, 'day' ).add( 1, 'hour' )
  } ]
} );

In this example we use moment for manage the timings, but you can also use the native Date object.

update

const agendaUid = 12345678;
const eventUid = 87654321;

const { event: updatedEvent } = await oa.events.update(
  agendaUid,
  eventUid,
  {
    slug: event.slug,
    title: {
      fr: 'Titre mise à jour',
      en: 'Updated title'
    },
    timings: event.timings
  }
);

delete

const agendaUid = 12345678;
const eventUid = 87654321;

await oa.events.delete(
  agendaUid,
  eventUid,
);

Locations

create

const agendaUid = 12345678;

const location = await oa.locations.create( agendaUid, {
  name: 'Gare Meuse TGV',
  address: 'Lieu dit Le Cugnet, 55220 Les Trois-Domaines',
  latitude: 48.9736458,
  longitude: 5.2723537
} );

Errors

Whatever the method if the error comes from the API the error will be in error.response and its JSON content in error.response.body.

If you use async / await then you can use try / catch otherwise you will have to use .catch.

async / await example:

try {
  await oa.events.create( 12345678, {
    slug: 'a-title',
    description: {
      fr: 'La description de votre événement',
      en: 'The description of your event'
    },
    locationUid: 87654321,
    timings: [ {
      begin: moment(),
      end: moment().add( 1, 'hour' )
    }, {
      begin: moment().add( 1, 'day' ),
      end: moment().add( 1, 'day' ).add( 1, 'hour' )
    } ]
  } );
} catch ( e ) {
  expect( e.response.body ).to.be.eql( {
    errors: [ {
      field: 'title',
      code: 'required',
      message: 'at least one language entry is required'
    } ]
  } );
}

Promise example:

oa.events.create( 12345678, {
  slug: 'a-title',
  description: {
    fr: 'La description de votre événement',
    en: 'The description of your event'
  },
  locationUid: 87654321,
  timings: [ {
    begin: moment(),
    end: moment().add( 1, 'hour' )
  }, {
    begin: moment().add( 1, 'day' ),
    end: moment().add( 1, 'day' ).add( 1, 'hour' )
  } ]
} )
  .then(result => {
    //
  })
  .catch(error => {
    expect( error.response.body ).to.be.eql( {
      errors: [ {
        field: 'title',
        code: 'required',
        message: 'at least one language entry is required'
      } ]
    } );
  });