ChatDaddy allows users to build scalable tools for businesses to empower their users to communicate via WhatsApp at the cheapest rates starting at just 1 USD/mo! For businesses to offer great services at affordable rates, they’d need access to an API that enables them to quickly add/remove users based on the requirements for each individual user.

For example, some users of your product may not require chat history and only send 500 messages a month whereas others send over 10000 messages a month!

This guide covers how to build a scalable platform using ChatDaddy’s APIs, that accommodates the usage of individual users. This is of course, in addition to all the features you get with the API, such as:

  1. Scaling without worrying about servers
  2. Generating “notes” in chats — notes are messages that never get sent to the other person in the chat
  3. Message queue that only allows a single message out at a time to prevent spam
  4. And so much more!

Before you can start building your scalable platform with the API, let’s get you access to the full platform

  1. Register for a ChatDaddy account if you haven’t already from https://app.chatdaddy.tech/SignUp

  2. Open ChatDaddy

    1. navigate to settings
    2. and then to billing
    3. or visit using (https://app.chatdaddy.tech/settings/billing)
  3. Click the “Upgrade Subscription” button

  4. Purchase the “API” plan

    Screenshot 2022-05-28 at 3.23.43 PM.png

    1. Continue to payment

    2. Enter your card details & finish the purchase

    3. You should be redirected back to ChatDaddy & see a “Successful Purchase” popup

    4. Refresh the page now and verify that you the newly purchased plan in the billing section like the image below

      Screenshot 2022-05-28 at 3.50.06 PM.png

  5. Superb! Now that you’ve full access to the platform, let’s get started with the API. We’d recommend using our typescript/javascript client to interact with our instant messaging service or if you choose, you could write your own REST client as well.

    1. typescript client: https://github.com/chatdaddy/typescript-client
    2. API docs: Stoplight

    For this guide, we’ll be using the typescript client from GitHub.

  6. Let’s setup a project to use API client now:

    1. We hope you have NodeJS tooling installed. If not, refer here
    2. Create a new project using NPM (you can use yarn too if you’d like) via npm init
    3. Add our API client to your project:
      1. npm i git+https://github.com/chatdaddy/typescript-client.git
    4. Create an empty file called src/index.js
    5. You can choose to use typescript as well, but for this example — we’ll just use plain JavaScript
  7. Let’s authenticate

    1. Inside index.js — add the following snippet that’ll allow us to authenticate our requests

      import { verifyToken, makeAccessTokenFactory, JWT, Scope } from '@chatdaddy/client'
      
      const REFRESH_TOKEN = 'put-your-refresh-token-here'
      const TEAM_ID = 'put-team-id'
      
      // the access token factory fetches access tokens as you'd need them 
      // using your refresh token, so that you can easily make requests to our services
      // without worrying about expired credentials or over-fetching access tokens
      const getAccessToken = makeAccessTokenFactory({
          request: { refreshToken: REFRESH_TOKEN }
      })
      
    2. If you don’t have a refresh token or don’t know your team ID, you can find it easily on the ChatDaddy web app: https://app.chatdaddy.tech/settings/api

  8. Now, let’s purchase & add your first user’s account (note: this step will create a 1USD charge on your account)

    1. We’ll use the AccountApi from the instant messaging service to purchase & add this account:

      import { AccountApi, AccountTier, AccountType, Configuration } from '@chatdaddy/client'
      
      (async() => {
      	const { token: accessToken } = await getAccessToken(TEAM_ID)
      	const accountsApi = new AccountApi(new Configuration({ accessToken }))
      	const { data } =  await accountsApi.accountsPost({
          accountsPostRequest: {
      			// just a nickname -- does not need to be unique
      			// this is just for display purposes
      			nickname: 'My New Account',
      			// change the tier to upgrade/downgrade functionality
      			// this one gives only limited messages, and syncs no chat history
      			tier: AccountTier.LimitedMsgNoChatHistory,
      			type: AccountType.Wa
          }
      	})
      
      	console.log(`purchases & created a new account with ID: "${data.accountId}"!`)
      })()
      
    2. Voila! You should now have another account in your team that you can assign to any one of your users!

    3. You can repeat this process as many times to scale as your user base grows!

  9. Impressed as you may be now, you likely have two questions now:

    1. If my user requires higher limits or needs chat history, how do I upgrade the ChatDaddy account?
    2. If my user unsubscribes, how will I unsubscribe the specific ChatDaddy account?

    We’ll tackle both now.

  10. First, let’s see how you can unsubscribe the account. We offer two ways:

    1. You can delete the account, removing all user data & cancelling the subscription in the process, with a simple API request:

      // account ID => the ID of the account you want to delete
      await accountsApi.accountsDelete({ accountId })
      
    2. You can “archive” the account, this will cancel your subscription for this account — logout the user from account, remove all synced data (will however, keep all notes & contacts)

      // account ID => the ID of the account to archive
      await accountsApi.accountsArchive({ accountId })
      
  11. Second, let’s see how you can upgrade/downgrade accounts:

    1. With one super simple API call, you can update the account’s tier

      // upgrades the tier to "unlimited messages & chat history"
      await accountsApi.accountsPatch({
      	accountId,
      	accountsPatchRequest: { tier: AccountTier.UnlimitedMsgChatHistory	}
      })
      
    2. If you try to change the tier of an account when it’s already at the same tier — the request will fail with a 400 Bad Request status code

    3. Policy on downgrading:

      1. When downgrading to a tier without chat history — all the chats, messages received at the time of login (scanning QR) will be removed
      2. If they choose to upgrade then, the user will have to re-scan the code to re-sync the history
      3. If they have crossed the 1000 message threshold — they will not be able to send any more messages till they upgrade or their usage gets reset at the start of the next billing period
  12. There we go, you’re now fully equipped to build a platform that can quickly & cheaply scale with your user base!